【问题标题】:Loop in a loop and If statement?循环循环和If语句?
【发布时间】:2017-11-28 23:28:33
【问题描述】:

我是一个初学者编程,我想用数组问多个问题,并告诉用户他每个问题是对还是错,我设法让它运行,但现在我如何实现代码以便用户最多只能尝试 3 次才能正确回答问题。

for(int n = 0; n <QArray.length; n++)
{
    System.out.println("Question" + (n+1));
    System.out.println(QArray[n]);

    String ans = scanner.nextLine();

    if (ans.equalsIgnoreCase(AArray[n]))
    {
        System.out.println("That is correct!");
    }
    else   
    {     
        System.out.println("That is incorrect!");
    }
}

【问题讨论】:

  • 您可以使用一个for 循环,最多计数为 3。
  • while (attempts &gt; 0) 然后每次递减attempts

标签: java arrays loops if-statement


【解决方案1】:

因此,如果我正确理解了您的目标,您有一个问题列表,虽然他们尝试失败的次数少于 3 次,但您希望用户尝试回答这些问题吗?

使用你现有的风格,你可以做类似的事情

for(int n = 0; n < QArray.length; n++)
{
    System.out.println("Question" + (n+1));
    System.out.println(QArray[n]);

    int incorrectAnswers = 0;
    while(incorrectAnswers < 3)
    {
        String ans = scanner.nextLine();
        if (ans.equalsIgnoreCase(AArray[n]))
        {
            System.out.println("That is correct!");
            break;
        }
        else   
        {     
            System.out.println("That is incorrect!");
            incorrectAnswers++;
        }
    }
}

根据数据的显示和传输方式以及对安全性等方面的担忧,拥有一个包含问题和答案的 QuestionAnswer 对象以及构成有效答案的方法(例如,不区分大小写,或者您可能想要接受多个单词等,无论您的情况如何),因此您最终可能会得到如下所示的代码。

for(int i = 0; i < questionAnswerArray.length; i++)
{
    QuestionAnswer qa = questionAnswerArray[i];

    System.out.println("Question " + (i+1));
    System.out.println(qa.getQuestion());

    int incorrectAnswers = 0;
    while(incorrectAnswers < 3)
    {
        String ans = scanner.nextLine();
        if (qa.isValidAnswer(ans))
        {
            System.out.println("That is correct!");
            break;
        }
        else   
        {     
            System.out.println("That is incorrect!");
            incorrectAnswers++;
        }
    }
}

【讨论】:

    【解决方案2】:

    尝试使用while 与您需要的尝试次数:

    for (int n = 0; n < QArray.length; n++) {
        System.out.println("Question" + (n + 1));
        System.out.println(QArray[n]);
        int attempt = 3;
    
        while (attempt > 0) {
            System.out.print("Please enter the answer: ");
            String ans = scanner.nextLine();
            if (ans.equalsIgnoreCase(AArray[n])) {
                System.out.println("That is correct!");
                break;
            } else {
                System.out.println("That is incorrect!");
            }
            attempt--;
        }
    }
    

    【讨论】:

      【解决方案3】:

      在第一个循环中放置第二个循环。

      for(int n = 0; n < QArray.length; n++) {
        boolean correct = false;
        for(int m = 0; m < 3; m ++) {
          String ans = scanner.nextLine();
          if (ans.equalsIgnoreCase(AArray[n])) {
            System.out.println("That is correct!");
            correct = true;
            break;
          } else {     
            System.out.println("That is incorrect!");
          }
        }
        if(!correct) {
          //something
        } else {
          //something else
        }
      }
      

      注意break;。当提交正确答案时,该命令将退出内部 for 循环(包含扫描仪输入)。如果用户在 3 次尝试中没有得到正确答案,则 for 循环将在到达其计数器的末尾时结束。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-06-09
        • 2013-10-18
        • 2013-02-06
        • 2016-01-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-01
        相关资源
        最近更新 更多