我正在做这个按行星计算重量的程序。我必须显示一个包含3列,行星名称,重力值和权重的图表。我正在尝试执行此程序,但我总是将重力值的输出设为NaN,并将所有重量值的输出设为0.0。
这是我的代码:
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
import java.util.InputMismatchException;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.FileWriter;
public class WeightOnPlanetsV1
{
public static double[] getGravity( ) throws IOException {
Scanner fileScanner = new Scanner(new File("output.txt"));
double[] Gravity = new double[8];
double[] abc = new double[8];
for(int i = 0; i < 8; i++)
{
abc[i] = fileScanner.nextDouble();
Gravity[i] = abc[i] / 9.79211720754188E34;
}
fileScanner.close();
return Gravity;
}
public static double[] calcWeight(){
double earthWeight =100.0;
double[] Weight = new double[8];
double[] Gravity = new double[8];
double[] mass = new double[8];
for(int a = 0; a < 8; a++){
mass[a] = (earthWeight * 433.59237) / Gravity[a];
Weight[a] = mass[a] * Gravity[a];
}
return Weight;
}
public static void PrintResults( ) throws IOException{
double[] Gravity = calcWeight();
double[] Weight = new double[8];
String[] planets = new String[8];
planets[0] = "Mercury";
planets[1] = "Venus";
planets[2] = "Earth";
planets[3] = "Mars";
planets[4] = "Jupiter";
planets[5] = "Saturn";
planets[6] = "Uranus";
planets[7] = "Neptune";
Scanner in = new Scanner(System.in);
System.out.print("Please enter your weight in pounds: ");
double weightonEarth = in.nextDouble();
System.out.println();
System.out.println(" My weight on the Planets ");
System.out.println();
System.out.printf("%10s %9s %8s", "Planet", "Gravity", "Weight on planet(lbs)");
System.out.println();
System.out.println("------------------------------------------- -------");
for(int y = 0; y < 8 ; y++)
{
System.out.printf("%-11s", planets[y]);
System.out.printf("%13.2f", Gravity[y]);
System.out.printf("%20.2f\n",Weight[y]);
}
}
}
这是我的输出值
3.681794655084276E34
8.863175348783772E34
9.79211720754188E34
3.6956542096917174E34
2.476892445877445E35
6.032365515665391E31
8.863553576713065E34
1.11374098023869E35
我真的很感激任何帮助!
答案 0 :(得分:1)
您的重力值是根据以下计算的:
double[] Gravity = calcWeight();
在calcWeight()
内,您返回一个double[] Weight
数组,该数组的计算公式为:
Weight[a] = mass[a] * Gravity[a];
正如@Thomas正确指出的那样,您实际上从未向Gravity[]
分配任何值,因为永远不会调用getGravity()
。因此,每个Weight
值的分母将为零,即这是除以零,我认为这是NaN
Gravity
值的原因。
在PrintResults()
内,永远不会定义Weight[]
数组,因此它们都是零。
如何修复代码:
您可以尝试使用以下内容:
public static double[] getGravity() throws IOException {
// same as before
}
// pass the already-computed gravity array to this method
public static double[] calcWeight(double[] gravity) {
double earthWeight = 100.0;
double[] weight = new double[8];
double[] mass = new double[8];
for (int a=0; a < 8; a++) {
mass[a] = (earthWeight * 433.59237) / gravity[a];
weight[a] = mass[a] * gravity[a];
}
return weight;
}
public static void PrintResults() throws IOException {
double[] Gravity = getGravity();
double[] Weight = calcWeight();
// keep the rest as is
}
答案 1 :(得分:0)
您没有在任何地方致电script is out of range
。相反,你正在为重力分配权重:
getGravity
通过用以下代码替换行来修复它:
double[] Gravity = calcWeight();