【问题标题】:I want to give the user a specified message but my program gives the user all the messages in java我想给用户一个指定的消息,但是我的程序给了用户所有的 java 消息
【发布时间】:2012-08-07 01:56:57
【问题描述】:

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

【问题讨论】:

    标签: java swing if-statement joptionpane


    【解决方案1】:

    使用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);
    }
    

    【讨论】:

    • 感谢@Vulcan 的回答,请在回答未来的homework 标记问​​题时查看FAQ on the homework tag
    • @BurhanKhalid 感谢您的帮助,但我知道它是如何工作的。我错误地没有注意到这是作业,直到我在我的答案被标记为接受后访问了该问题。
    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:
      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);
      }`
      

      【讨论】:

        猜你喜欢
        • 2019-06-08
        • 1970-01-01
        • 1970-01-01
        • 2022-01-02
        • 2012-04-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多