2016-01-14 96 views
-1
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); 
    } 

我試圖運行一個程序,它會在不同的行星上返回你的體重。除了我的任意星球,一切都在運轉。如果您在void main(string[]args)中鍵入"arbitrary",它會詢問"What is your weight"並停止運行參數。它說我有一個NumberFormatException。我怎樣才能解決這個問題?數字格式異常?

+2

您忘記了解更多信息。如果您使用的是掃描儀,那麼在每個問題之後,您應該執行'massPerson = scanner.nextDouble();' –

+1

您實際上並未接受來自用戶的任何輸入。你只是試圖以「Double」的形式反覆解析「任意」。 – tnw

+0

我該如何解決這個問題? @tnw –

回答

-1

如果您的字符串輸入等於任意,它不能是數字的表示,那麼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); 
    } 
+0

這並沒有解決問題,因爲代碼在每個問題後仍然不接受輸入。 – tnw

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 
}