2013-10-22 42 views
0

我正在此作爲錯誤環將不顯示結果.NumberFormatException.forInputString(未知來源)
在java.lang.Integer.parseInt(未知來源)
在java.lang.Integer.parseInt(未知來源)
在exercises.GPACalculator.main(GPACalculator.java:53 )而當用戶輸入「Q」

public static void main(String[] args) 
{ 
      String input = ""; 
    String letterGrade = ""; 
    String creditHour = ""; 
    int credits = 0; 
    int points = 0; 
    int course = 1; 
    int gpa; 
    String grade = ""; 

    String greeting = "Hello, this program will calculate your GPA. You will be asked \n"+ 
      "to enter your letter grade for each class, then you will be asked to enter \n"+ 
      "the corresponding number of credits for that class. Once all the grades and \n"+ 
      "credits have been entered, the program will display your GPA."; 
    JOptionPane.showMessageDialog(null,greeting,"Greeting - Introduction",1); 

    String gradePrompt = "Enter your letter grade (A, B, C, D, F)\n"+ 
      "Press \"Q\" to quit and display your results"; 
    String creditPrompt = "Enter the credit hours for course "+course+" (0, 1, 2, 3, 4, 5, 6)\n\n"+ 
      "Press \"Q\" to quit and display your results"; 

    do  
    { 
     input = JOptionPane.showInputDialog(null,gradePrompt,"Enter grade",1); 
     letterGrade += input.toUpperCase(); 

     input = JOptionPane.showInputDialog(null,creditPrompt,"Enter credit hours",1); 
     creditHour += input.toUpperCase(); 
     course++; 
     if(input.toUpperCase().equals("Q")) 
      continue; 
      credits = Integer.parseInt(input); 

      switch (grade) { 
      case "A": points = 4; 
       break; 
      case "B": points = 3; 
       break; 
      case "C": points = 2; 
       break; 
      case "D": points = 1; 
       break; 
      case "F": points = 0; 
       break; 
      } 

     //input = JOptionPane.showInputDialog(null,"Do you want to quit? (Press Q "+ 
     //  "to quit, no to continue)" ,"Do you want to quit?",1); 
    }while(!input.toUpperCase().equals("Q")); 

    input = input.substring(0,input.length()-1); 

    gpa = points/credits; 

    String results = "The courses you entered are:\n\n"+ 
      "Grade "+"Hours \n\n"+ 
      letterGrade+" \n\n"+creditHour+"\n\n"+ 
      "Resulting in a GPA of "+gpa+"\n\n"+ 
      "This program will now terminate!"; 

    JOptionPane.showMessageDialog(null, new JTextArea(results), 
      "GPA results",1); 
} 

}

回答

0

由於堆棧跟蹤顯示它不能將字符串轉換爲整數這裏:

credits = Integer.parseInt(input); 

while循環檢查,如果輸入了值「Q」。但問題是,你更新了代碼本身內部的輸入值。因此,當您提供值Q時,循環將完成當前迭代並在檢查下一次迭代時中斷。我建議你在循環結尾處輸入另一個輸入。

input = JOptionPane.showInputDialog(null,"Do you want to quit? Press Q" ,"Do you want to quit? Press Q ",1); 
+0

現在我得到的錯誤: 異常線程 「main」 java.lang.StringIndexOutOfBoundsException:字符串索引超出範圍:-1 \t在java.lang.String.substring(來源不明) \t在exercise.GPACalculator.main(GPACalculator.java:75) – user2908744

+0

是的..應該在這一行 - input.substring(0,input.length() - 2); ..這條線看起來有問題。你沒有得到它,因爲執行沒有到達這條線 –

+0

謝謝你的工作,但爲什麼它沒有顯示結果中的每個年級/學分?它只顯示「Q」和「3」。 – user2908744

2

問題是這部分

String creditPrompt = "Enter the credit hours for your course (0, 1, 2, 3, 4, 5, 6)\n"+ 
      "Enter Q to display your results\n\n";  
input = JOptionPane.showInputDialog(null,creditPrompt,"Enter grade",1); 
credits = Integer.parseInt(input); 

您試圖解析 「Q」 爲int。你應該先檢查輸入是不是「Q」,然後解析輸入拿到學分

0

credits = Integer.parseInt(input);

您在輸入轉換爲整數。如果輸入是不是數字的「Q」,則NumberFormatException將被拋出。要麼你需要給你的輸入一個數字或者在你的代碼中處理NumberFormatException。

相關問題