if (input.equals("arbitrary"))
{
System.out.println("What is your mass?");
massPerson = Double.parseDouble(input);
System.out.println("What is the planet's weight?");
massPlanet = Double.parseDouble(input);
System.out.println("What is the radius of the planet?");
radiusPlanet = Double.parseDouble(input);
weight = arbitraryPlanetWeight(massPerson, massPlanet, radiusPlanet);
}
我试图运行一个程序,让它在不同的星球上重量。除了我任意的行星,一切都在起作用。如果您在"arbitrary"
中输入void main(string[]args)
,则会询问"What is your weight"
并停止运行参数。它说我有NumberFormatException
。我该如何解决这个问题?
答案 0 :(得分:0)
我已经写了一个我假设你要做的事情的例子。 阅读所有评论,他们应该帮助您理解代码。
public static void main(String[] args) {
/*args is a array of String so for example:
["arbitrary"] -> array of length == 1
or
["12.2","11.1","13.3"]
*/
//If there were no elements in the array this code would break but we will assume there is at least one
String input = args[0];//put the first element in the array into input
double weight,massPerson,massPlanet,radiusPlanet;//define variables that we will use
if (input.equals("arbitrary"))//we look is the string equal to "arbitrary"
{
System.out.println("What is your mass?");
//The next commented out line if it where a command could not pass here because you are trying to interpret "arbitrary" as a double value
//double massPerson = Double.parseDouble(input);
//instead you want to user to input the value
Scanner s = new Scanner(System.in);
input = s.nextLine(); //now input is what the user has entered
//we assume that what the user has entered is a number
//if the user input something like "hello" the code would break and you would again get the NumberFormatException
massPerson = Double.parseDouble(input);
System.out.println("What is the planet's weight?");
input = s.nextLine();
massPlanet = Double.parseDouble(input);
System.out.println("What is the radius of the planet?");
input = s.nextLine();
radiusPlanet = Double.parseDouble(input);
s.close();//it is important to close the scanner -> there is also a try syntax to do this closing automatically so google : " try-with-resources Statement"
}
else{//we have something else as the input
//lets assume then that the args[] has 3 values which can all be parsed
//also since we have the values there is no need to ask the user
massPerson = Double.parseDouble(args[0]);//take the first value in the args array and pars it
massPlanet = Double.parseDouble(args[1]);//take the second value in the args array and pars it
radiusPlanet = Double.parseDouble(args[2]);//take the third value in the args array and pars it
}
//we have all the values at this point so we can calculate the weight
weight = arbitraryPlanetWeight(massPerson, massPlanet, radiusPlanet);
}
private static double arbitraryPlanetWeight(double massPerson, double massPlanet, double radiusPlanet) {
System.out.println("Do some calculations");
return 50;//dummy result
}
答案 1 :(得分:-1)
如果你的String输入等于任意,它不能表示数字,那么Double.parseDouble(输入)会启动一个解析异常。
您应该为每个所需信息设置变量:
if (input.equals("arbitrary"))
{
System.out.println("What is your mass?");
massPerson = Double.parseDouble(massPersonStr);
System.out.println("What is the planet's weight?");
massPlanet = Double.parseDouble(massPlanetStr);
System.out.println("What is the radius of the planet?");
radiusPlanet = Double.parseDouble(radiusPlanetStr);
weight = arbitraryPlanetWeight(massPerson, massPlanet, radiusPlanet);
}