该程序应弹出两个对话框。一种告诉您输入“学生类型”(数字1、2或3),第二种要求您输入学生成绩。

我运行它,只得到第一个对话框,它运行良好,我输入了一个类型(1到3),并且应该得到一个第二个对话框,其中显示了录入成绩的条目。

为什么第二个框没有显示?它与我的第一个对话框类型相同。使用第一个对话框后,我的程序退出。谢谢您的帮助,我是Java的初学者。

import javax.swing.JOptionPane;


public class Exercise4_8 {

    public static void main(String[] args) {

        final double ENGLISH_MIDTERM_PERCENTAGE = .25;
        final double ENGLISH_FINALEXAM_PERCENTAGE = .25;
        final double ENGLISH_RESEARCH_PERCENTAGE = .30;
        final double ENGLISH_PRESENTATION_PERCENTAGE = .20;

        final double MATH_MIDTERM_PERCENTAGE = .50;
        final double MATH_FINALEXAM_PERCENTAGE = .50;

        final double SCIENCE_MIDTERM_PERCENTAGE = .40;
        final double SCIENCE_FINALEXAM_PERCENTAGE = .40;
        final double SCIENCE_RESEARCH_PERCENTAGE = .20;


        int midterm = 0;
        int finalExamGrade = 0;
        int research = 0;
        int presentation = 0;
        double finalNumericGrade = 0;
        String finalLetterGrade = "";
        String response;
    //What type of student are we calculating?
        response = JOptionPane.showInputDialog("Enter student type (1=English, 2=Math, 3=Science)");

        if(response == null){
            JOptionPane.showMessageDialog(null, "You clicked on the Cancel button");

        }
        else if(response.equals("")){
            JOptionPane.showMessageDialog(null, "You must make an entry in the InputBox");

        }
        else if(Integer.parseInt(response) < 1 || Integer.parseInt(response) > 3) {
            JOptionPane.showMessageDialog(null, response + " - is not a valid student type");
            System.exit(0);

    //Student type is valid, now let's calculate grade
        switch(Integer.parseInt(response)){
    //Case 1 is an English Student
        case 1:
            midterm = Integer.parseInt(JOptionPane.showInputDialog("Enter the Midterm Grade"));
            finalExamGrade = Integer.parseInt(JOptionPane.showInputDialog("Enter the Final Examination Grade"));
            research = Integer.parseInt(JOptionPane.showInputDialog("Enter the Research Grade"));
            presentation = Integer.parseInt(JOptionPane.showInputDialog("Enter the Presentation Grade"));
            finalNumericGrade = (midterm * ENGLISH_MIDTERM_PERCENTAGE) +
                    (finalExamGrade * ENGLISH_FINALEXAM_PERCENTAGE) +
                    (research * ENGLISH_RESEARCH_PERCENTAGE) +
                    (presentation * ENGLISH_PRESENTATION_PERCENTAGE);

        if(finalNumericGrade >=93)
            finalLetterGrade = "A";
        else
            if((finalNumericGrade >= 85) && (finalNumericGrade < 93))
                finalLetterGrade = "B";
        else
            if((finalNumericGrade >= 78) && (finalNumericGrade < 85))
                finalLetterGrade = "C";
        else
            if((finalNumericGrade >= 70) && (finalNumericGrade < 78))
                finalLetterGrade = "D";
        else
            if (finalNumericGrade < 70)
                finalLetterGrade = "F";

        JOptionPane.showMessageDialog(null, "***ENGLISH STUDENT***\n\n" +
                "Mideterm grade is: " + midterm + "\n" +
                "Final Exam is: " + finalExamGrade + "\n" +
                "Research grade is: " + research + "\n" +
                "Presentation grade is: " + presentation + "\n\n" +
                "Final Numeric Grade is: " + finalNumericGrade + "\n" +
                "Final Letter Grade is: " + finalLetterGrade);
            break;
    //Case 2 is a Math Student
        case 2:
            midterm = Integer.parseInt(JOptionPane.showInputDialog("Enter the Midterm Grade"));
            finalExamGrade = Integer.parseInt(JOptionPane.showInputDialog("Enter the Final Examination Grade"));
            finalNumericGrade = (midterm * MATH_MIDTERM_PERCENTAGE) +
                    (finalExamGrade * MATH_FINALEXAM_PERCENTAGE);

            if(finalNumericGrade >= 90)
                finalLetterGrade = "A";
            else
                if((finalNumericGrade >= 83) && (finalNumericGrade < 90))
                    finalLetterGrade = "B";
            else
                if((finalNumericGrade >= 76) && (finalNumericGrade < 83))
                    finalLetterGrade = "C";
            else
                if((finalNumericGrade >=65) & (finalNumericGrade < 76))
                    finalLetterGrade = "D";
            else
                if(finalNumericGrade <65)
                    finalLetterGrade = "F";
                JOptionPane.showMessageDialog(null, "***MATH STUDENT***\n\n" +
                    "Midterm grade is: " + midterm + "\n" +
                    "Final Exam is: " + finalExamGrade + "\n\n" +
                    "Final Numeric Grade is: " + finalNumericGrade + "\n" +
                    "Final Letter Grade is: " + finalLetterGrade);
            break;
    //Case 3 is a Science Student
        case 3:
            midterm = Integer.parseInt(JOptionPane.showInputDialog("Enter the Midterm Grade"));
            finalExamGrade = Integer.parseInt(JOptionPane.showInputDialog("Enter the Final Examination Grade"));
            research = Integer.parseInt(JOptionPane.showInputDialog("Enter the Research Grade"));
            finalNumericGrade = (midterm * SCIENCE_MIDTERM_PERCENTAGE) +
                    (finalExamGrade * SCIENCE_FINALEXAM_PERCENTAGE) +
                    (research * SCIENCE_RESEARCH_PERCENTAGE);

            if(finalNumericGrade >= 90)
                finalLetterGrade = "A";
            else
                if((finalNumericGrade >= 80) && (finalNumericGrade < 90))
                    finalLetterGrade = "B";
            else
                if((finalNumericGrade >= 70) && (finalNumericGrade < 80))
                    finalLetterGrade = "C";
            else
                if((finalNumericGrade >= 60) && (finalNumericGrade <70))
                    finalLetterGrade = "D";
            else
                if(finalNumericGrade < 60)
                    finalLetterGrade = "F";

            JOptionPane.showMessageDialog(null, "***SCIENCE STUDENT***\n\n" +
                "Midterm grade is: " + midterm + "\n" +
                "Final Exam is: " + finalExamGrade + "\n" +
                "Research grade is: " + research + "\n\n" +
                "Final Numeric Grade is: " + finalNumericGrade + "\n" +
                "Final Letter Grade is: " + finalLetterGrade);
            break;
        default:
            JOptionPane.showMessageDialog(null, response + " - is not a valid student type");
            System.exit(0);
        }
    System.exit(0);
    }
}
}

最佳答案

关闭最后一个else if括号{

else if(Integer.parseInt(response) < 1 || Integer.parseInt(response) > 3) {
    JOptionPane.showMessageDialog(null, response + " - is not a valid student type");
    System.exit(0);
}


并将else放在其下面:

else{


这应该可以解决您的问题。

10-04 18:57