缺少双数组

时间:2013-12-29 21:09:51

标签: java

我的输出有问题。当我运行程序时,我得到一个打印出零的数组。其中一些变量应该介于0和1之间。但我将所有变量都归零。我还有另一个问题,但是我不确定是否应该在同一个帖子上提出两个问题以及在哪里标记这个问题。(这是关于等式的数学计算。) 输出应该如下所示:this 回顾一下我的问题是为什么我的双列阵打印为零。 输入是:

  • 3700465.457603474
  • 8866199.9605471
  • 9794740.68167652
  • 3697967.6848667157
  • 2.4776754466506414E7
  • 1.04381​​4587026926E7
  • 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

}

1 个答案:

答案 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的调试器。它是开发人员最重要的工具吗?我在调试器的两分钟内发现了这个错误,并逐步完成了代码。