【问题标题】:Java while loop Quiz doesn't resetJava while 循环测验不会重置
【发布时间】:2020-05-27 19:22:22
【问题描述】:

作为我课程作业的一部分,我的任务是创建一个简单的问答游戏,该游戏会循环直到用户选择正确的答案。 当答案正确(答案为 B)或不正确时,我可以生成消息提示和响应提示,但我正在努力让 while 循环重置并再次提示用户。 目前,当用户得到错误答案时,它会在响应提示上无限循环。

import javax.swing.JOptionPane;
public class Quiz {      

public static void main(String[] args) {
    String question =  "What colour is the sky?\n";
     question += "A. Purple\n";
     question += "B. Blue\n";
     question += "C. Green\n";
     question += "D. Yellow\n";
     question += "E. Orange\n";

     String answer = JOptionPane.showInputDialog(question);
     answer = answer.toUpperCase();

     int guess = 0;
     while (answer != "B")

     if (answer.equals("B")) {
         JOptionPane.showMessageDialog(null,"Correct!");
         break;
        }
     else if (answer.equals("A")) {
         JOptionPane.showMessageDialog(null,"Incorrect. Please try again!");
        }
     else if (answer.equals("C")) {
         JOptionPane.showMessageDialog(null,"Incorrect. Please try again!");
        }
     else if (answer.equals("D")) {
         JOptionPane.showMessageDialog(null,"Incorrect. Please try again!");
        }
     else if (answer.equals("E")) {
         JOptionPane.showMessageDialog(null,"Incorrect. Please try again!");
        }
     else {
         JOptionPane.showMessageDialog(null, "Invalid answer. Please enter A, B, C, D, or E.");
        }
     guess++;
}
}

【问题讨论】:

  • guess++; 不是while 循环的一部分,仅供参考。
  • 还有while (answer != "B")if (answer.equals("B"))这些说法是矛盾的。
  • 在循环中使用方括号 ({ ...})
  • 在您的 while 循环中,您永远不会更改变量 answer 的值。所以循环要么根本不运行,要么无限次运行。
  • 如果你用equals检查相等,为什么你用!=检查不相等?看看switch case

标签: java string while-loop


【解决方案1】:

你忘了给机会再次回答这个问题。 现在它正在按您的预期工作。

    public static void main(String[] args) {
    String question =  "What is the Capital of South Africa?\n";
    question += "A. Cape town\n";
    question += "B. Pretoria\n";
    question += "C. Johannesburg\n";
    question += "D. Durban\n";
    question += "E. Nelspruit\n";

    String answer = JOptionPane.showInputDialog(question);
    answer = answer.toUpperCase();

    int guess = 0;
    while (answer != "B") {
        answer = JOptionPane.showInputDialog(question);
        answer = answer.toUpperCase();
        if (answer.equals("B")) {
            JOptionPane.showMessageDialog(null, "Correct!");
            break;
        } else if (answer.equals("A")) {
            JOptionPane.showMessageDialog(null, "Incorrect. Please try again!");
        } else if (answer.equals("C")) {
            JOptionPane.showMessageDialog(null, "Incorrect. Please try again!");
        } else if (answer.equals("D")) {
            JOptionPane.showMessageDialog(null, "Incorrect. Please try again!");
        } else if (answer.equals("E")) {
            JOptionPane.showMessageDialog(null, "Incorrect. Please try again!");
        } else {
            JOptionPane.showMessageDialog(null, "Invalid answer. Please enter A, B, C, D, or E.");
        }
        guess++;
    }
}

【讨论】:

  • 一些建议将answer = JOptionPane.showInputDialog(question); answer = answer.toUpperCase();放在while循环的末尾,while (answer != "B")也应该是while("B".equals(answer))
【解决方案2】:

而不是..

while (answer != "B"){

         answer = JOptionPane.showInputDialog(question);
         answer = answer.toUpperCase();

         if (answer.equals("B")) {
             JOptionPane.showMessageDialog(null,"Correct!");
             break;
            }
         else if (answer.equals("A")) {
             JOptionPane.showMessageDialog(null,"Incorrect. Please try again!");
            }
         else if (answer.equals("C")) {
             JOptionPane.showMessageDialog(null,"Incorrect. Please try again!");
            }
         else if (answer.equals("D")) {
             JOptionPane.showMessageDialog(null,"Incorrect. Please try again!");
            }
         else if (answer.equals("E")) {
             JOptionPane.showMessageDialog(null,"Incorrect. Please try again!");
            }
         else {
             JOptionPane.showMessageDialog(null, "Invalid answer. Please enter A, B, C, D, or E.");
            }
         guess++;
}

你错过了大括号。

【讨论】:

  • 而不是什么?另外,如果你指的是大括号,那不是唯一的问题。
  • while (answer != "B") 应该是while("B".equals(answer))
  • @KarthikeyanVaithilingam 哦,我错过了你是对的!字符串值必须使用 equals 函数。
【解决方案3】:

如果用户输入的不是B,你必须一次又一次地提示问题。在 while 循环中,您需要更改变量 answer 的值。试试下面的工作 sn-p。

public class Quiz{

    public static void main(String[] args) throws IOException {

        String answer = promptQuestion();

        while (answer != "B") {
            if (answer.equals("B")) {
                JOptionPane.showMessageDialog(null, "Correct!");
                break;
            } else if (answer.equals("A")) {
                JOptionPane.showMessageDialog(null, "Incorrect. Please try again!");
                answer = promptQuestion();
            } else if (answer.equals("C")) {
                JOptionPane.showMessageDialog(null, "Incorrect. Please try again!");
                answer = promptQuestion();
            } else if (answer.equals("D")) {
                JOptionPane.showMessageDialog(null, "Incorrect. Please try again!");
                answer = promptQuestion();
            } else if (answer.equals("E")) {
                JOptionPane.showMessageDialog(null, "Incorrect. Please try again!");
                answer = promptQuestion();
            } else {
                JOptionPane.showMessageDialog(null, "Invalid answer. Please enter A, B, C, D, or E.");
                answer = promptQuestion();
            }
        }
    }

    public static String promptQuestion() {
        String question = "What is the Capital of South Africa?\n";
        question += "A. Cape town\n";
        question += "B. Pretoria\n";
        question += "C. Johannesburg\n";
        question += "D. Durban\n";
        question += "E. Nelspruit\n";

        String answer = JOptionPane.showInputDialog(question);
        return answer.toUpperCase();
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-13
    • 1970-01-01
    • 1970-01-01
    • 2010-11-26
    • 1970-01-01
    • 2019-03-02
    • 2020-11-26
    相关资源
    最近更新 更多