Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 重复循环的增量_Java - Fatal编程技术网

Java 重复循环的增量

Java 重复循环的增量,java,Java,对于我的科学展项目,我想给一个法语教学程序提供一个图形更新,这个程序太旧了,以至于在DosBOX中被模拟。这很好,但我有问题。我在一个控制台应用程序中编写程序的基本逻辑,就是为了把它们结合起来。我创建了一个问题类,它位于一个名为“test1”的数组列表/集合中 我有一个循环,循环遍历列表,每次迭代都会运行另一个名为evaluate的方法: public static boolean evaluate(Question question, Scanner scanner) { System

对于我的科学展项目,我想给一个法语教学程序提供一个图形更新,这个程序太旧了,以至于在DosBOX中被模拟。这很好,但我有问题。我在一个控制台应用程序中编写程序的基本逻辑,就是为了把它们结合起来。我创建了一个问题类,它位于一个名为“test1”的数组列表/集合中

我有一个循环,循环遍历列表,每次迭代都会运行另一个名为evaluate的方法:

public static boolean evaluate(Question question, Scanner scanner)
{
    System.out.println(question.getPhrase()); // prints the question
    String answer = scanner.nextLine(); // gets the answer
    if (answer.compareTo(question.getCorrectAnswer()) == 0)
        return true; // compares the answer to the correct answer w/i the current instance of "Question"
    else
        return false; // if it's not right, returns "false" meaning the question wasn't correct
}
循环看起来是这样的:

    for (Question question : test1)
    {
        if (evaluate(question, scan))
            {
                incorrect = 0;
                continue;
            }

        else
            {
                incorrect++;
                System.out.println(incorrect);
            }

        if (incorrect == 3)
            System.out.println("you have failed");
            break;
    }
我想这样做,如果你回答一个错误的问题,它会再次吐出这个短语,并将“不正确”增加1,如果你点击3,则会终止列表(如果我能让它重复这个问题,我想我已经正确地实现了)。现在,它移动到列表中的下一项,因此是下一个问题,即使我不希望这样


抱歉,如果我的代码很糟糕,我对编程还是比较陌生的。

在for循环中创建一个while循环,表示如果问题回答不正确,然后重复,这样每个问题都会被问到,直到问题正确为止。将应创建的for循环中的所有内容保留在while循环中:

for (Question question : test1)
{
    while(!evaluate(question, scan)) {
    if (evaluate(question, scan))
        {
            incorrect = 0;
            continue;
        }

    else
        {
            incorrect++;
            System.out.println(incorrect);
        }

    if (incorrect == 3)
        System.out.println("you have failed");
        break;
}
}

您可以执行以下操作,而不是像现在这样执行
foreach
循环:

for (int i = 0; i < test1.size(); i++) {
    Question question = test1.get(i);
    if (evaluate(question, scan)) {
        ...
    } else {
        incorrect++;
        test1.add(question);
    }

    if (incorrect == 3) { ... }
}
您还可以为
else
案例嵌套一个循环:

for (int i = 0; i < test1.size(); i++) {
    Question question = test1.get(i);
    if (evaluate(question, scan)) {
        ...
    } else {
        incorrect++;
        i--;
    }

    if (incorrect == 3) { ... }
}
for (Question question : test1) {
    boolean passed = True;
    incorrect = 0;
    while (!evaluate(question, scan)) {
        incorrect++;
        if (incorrect == 3) { passed = False; break; }
    }

    if (!passed) { System.out.println("you have failed"); break; }
}

你需要在你已经拥有的循环中加入另一个循环,重复当前的问题,直到正确回答。这并没有完全按照我所希望的那样工作,但它仍然为我提供了关于如何在将来可能实现类似问题的输入,所以谢谢你!我没有那样想过。我知道我需要第二个循环,但不确定该怎么做。