在我的程序中,用户输入一个数字,下面的方法计算该数字并根据他们输入的数字向用户提供一条消息,但是我使用 if 语句来计算要在该数字上给出什么消息,但是当用户输入它显示所有消息的数字,数字以 10 为一组,这意味着如果用户输入 40 到 49 之间的数字,那么它将输出 E 级消息,有人可以告诉我如何制作它以便它只给我与它比较的指定数字的消息?
public void checkInputScore() {
if (convertedInputScore == -1) {
System.exit(0);
}
if (convertedInputScore < 39) {
JOptionPane.showMessageDialog(Program1.this, "The student received a fail grade", "Student mark checker", JOptionPane.INFORMATION_MESSAGE);
}
if (convertedInputScore <= 49) {
JOptionPane.showMessageDialog(Program1.this, "The student received an E grade", "Student mark checker", JOptionPane.INFORMATION_MESSAGE);
}
if (convertedInputScore <= 59) {
JOptionPane.showMessageDialog(Program1.this, "The student received an D grade", "Student mark checker", JOptionPane.INFORMATION_MESSAGE);
}`
最佳答案
使用 else if
语句。
if (convertedInputScore == -1) {
System.exit(0);
} else if (convertedInputScore < 39) {
JOptionPane.showMessageDialog(Program1.this, "The student received a fail grade", "Student mark checker", JOptionPane.INFORMATION_MESSAGE);
} else if (convertedInputScore <= 49) {
JOptionPane.showMessageDialog(Program1.this, "The student received an E grade", "Student mark checker", JOptionPane.INFORMATION_MESSAGE);
} else if (convertedInputScore <= 59) {
JOptionPane.showMessageDialog(Program1.this, "The student received an D grade", "Student mark checker", JOptionPane.INFORMATION_MESSAGE);
}
关于java - 我想给用户一个指定的消息,但我的程序给了用户所有的 java 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11838320/