我的输出有问题。当我运行程序时,我得到一个打印出零的数组。其中一些变量应该介于0和1之间。但我将所有变量都归零。我还有另一个问题,但是我不确定是否应该在同一个帖子上提出两个问题以及在哪里标记这个问题。(这是关于等式的数学计算。) 输出应该如下所示:this 回顾一下我的问题是为什么我的双列阵打印为零。 输入是:
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
}
答案 0 :(得分:4)
您的重力值(在“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的调试器。它是开发人员最重要的工具吗?我在调试器的两分钟内发现了这个错误,并逐步完成了代码。