1

在我的程序中,用户输入一个数字,下面的方法计算数字并根据他们输入的数字给用户一条消息,但是我使用 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);
    }`
4

3 回答 3

3

使用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);
}
于 2012-08-07T01:59:03.013 回答
2
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);            
} 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);
}`
于 2012-08-07T01:59:48.890 回答
2

小于 39 的数字也将小于 49;并且由于第一次检查之后的每一次检查if都是正确的,因此您的程序会显示所有消息。

这应该可以帮助您解决家庭作业问题。

于 2012-08-07T02:00:19.360 回答