我的输出有问题。当我运行该程序时,我得到一个打印出零的数组。其中一些变量应该介于 0 和 1 之间。但我将所有变量都设置为零。我还有一个关于程序的问题,但我不确定我是否应该在同一个帖子上问两个问题以及去哪里标记这个问题。(这是关于方程式的数学。) 输出应该如下所示:this 回顾一下我的问题是为什么我的双数组被打印为零。 输入是:
- 3700465.457603474
- 8866199.9605471
- 9794740.68167652
- 3697967.6848667157
- 2.4776754466506414E7
- 1.043814587026926E7
- 8861473.215210252
- 1.1131680688084042E7
- 612807.1987535232
public class Weight1 {
public static double[] gravity (double[] z) throws IOException
{
File fileName= new File("gravity.txt");
Scanner inFile= new Scanner(fileName);
for( int i=0; i<z.length; i++)
{
z[i]= inFile.nextDouble();
}
return z;
}
public static double [] realgravity (double []z, double[] o) {
for ( int bo=0; bo<z.length; bo++)
{
o[bo]= z[bo]*6.67E-11;
}
return o;
}
public static double[] realweight(double[] calcWeight, double earthWeight,double[] o)
{
for ( int bit=0; bit<calcWeight.length;bit++)
{
calcWeight[bit]= earthWeight/o[bit];
}
return calcWeight;
}
public static void printResults(String[] names, double [] o, double[] calcWeight) throws IOException
{
System.out.println(" My weight on planets:");
System.out.println(" Planet Gravity Weight(lbs)");
System.out.println("=====================================");
for (int iq=0; iq<o.length; iq++)
{
System.out.printf( "%1s " , names[iq]);
System.out.printf( "%8.2f", o[iq]);
System.out.printf("%17.1f",calcWeight[iq]);
System.out.printf("%n");
}
}
// Note: formula for finding weight on a planet: Earth weight divided by Gravity constant times surface gravity
public static void main(String[] args)throws IOException
{
double earthWeight = 100.0; // initalize Earth weight to 100 lbs
double [] z= new double [9];
double [] o= new double [9];
double [] calcWeight= new double [9];
String printResults;
double coin[]= gravity(z);
double bit[]= realgravity(z,o);
double next[]=realweight(calcWeight,earthWeight,o);
String[] names = {"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune","Pluto"};
printResults(names,o,calcWeight);
// static method you write
// static method you write
}
最佳答案
你的重力值(在“o”数组中)是非常小的数字:
[0] 2.4682104602215173E-4
[1] 5.913755373684916E-4
[2] 6.533092034678239E-4
[3] 2.466544445806099E-4
[4] 0.0016526095229159779
然后使用格式说明符将它们打印出来:
System.out.printf( "%8.2f", o[iq]);
将所有值四舍五入到小数点后两位;因此,0.00 值。所以你的计算有误。更正留给你去尝试,建议 - 学习如何使用你的 IDE 的调试器。它是开发人员最重要的工具。我用调试器在两分钟内发现了这个错误,并单步执行了代码。
关于java - 缺少双阵列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20830090/