这个程序中应该弹出2个对话框。一个告诉你输入“学生类型”(数字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);
}
}
}
答案 0 :(得分:2)
关闭您的上一个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{
这可以解决您的问题。
答案 1 :(得分:0)
这是你的工作代码检查它:
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);
}
}
你在关闭elseif(Integer.parseInt(response) < 1 || Integer.parseInt(response) > 3)
条件