【问题标题】:While loop will not display results when user enters "Q" [closed]当用户输入“Q”时,while循环不会显示结果[关闭]
【发布时间】:2013-10-22 20:30:14
【问题描述】:

这是一个错误:
线程“main”中的异常 java.lang.NumberFormatException:对于输入字符串:“q”
在 java.lang.NumberFormatException.forInputString(未知来源)
在 java.lang.Integer.parseInt(未知来源)
在 java.lang.Integer.parseInt(未知来源)
在练习.GPACalculator.main(GPACalculator.java:53)

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);
}

}

【问题讨论】:

    标签: java while-loop switch-statement exit-code


    【解决方案1】:

    问题出在这部分

    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”,然后解析输入以获得学分

    【讨论】:

      【解决方案2】:

      正如堆栈跟踪所示,它无法在此处将字符串转换为整数:

      credits = Integer.parseInt(input);
      

      您的 while 循环检查输入是否具有值“Q”。但问题是您更新了 while 代码本身内的输入值。因此,当您提供值 Q 时,循环将完成当前迭代并在检查下一次迭代时中断。我建议您在循环末尾添加另一个输入。

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

      【讨论】:

      • 现在我得到错误:线程“main”中的异常 java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:-1 at java.lang.String.substring(Unknown Source) at exercise.GPACalculator .main(GPACalculator.java:75)
      • Yes.. 应该在这一行 - input.substring(0,input.length()-2);.. 这行看起来有问题。你没有早点得到它,因为执行没有到达这条线
      • 谢谢你的工作,但为什么它不在结果中显示每个年级/学分?它只显示“Q”和“3”。
      • 因为您只有一个变量来存储您在最后打印的成绩/学分。在每次迭代中,这些变量都会被修改,最终值是循环最后一次迭代期间的值。如果要查看所有成绩,请将成绩更改为 int 数组或 int ArrayList。与学分相同。
      • 感谢您的帮助!
      【解决方案3】:

      credits = Integer.parseInt(input);

      您正在将输入转换为整数。如果输入是不是数字的“Q”,则会抛出 NumberFormatException。您需要在代码中输入数字或处理 NumberFormatException。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-07-14
        • 1970-01-01
        • 2023-03-16
        • 2019-08-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-02-10
        相关资源
        最近更新 更多