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 に答える