如何跳出第二个循环(嵌套)并继续循环

How to break out of 2nd loop(nested) and continue looping

我的 Java 程序需要帮助。我是初学者,所以这对我来说真的很困惑。我正在尝试构建一个多问题琐事程序。因此,系统会提示用户一个问题,他们必须从给定的答案中进行选择。如果他们做对或错了,他们会被告知,并且还会显示他们回答问题所花费的时间。现在的问题是,在他们提交了对一个问题的回答后,他们会被提示是否想再玩一次,然后会出现另一个问题。我有 2 个列表 1 个问题和 1 个答案我同时使用 2 个 for 循环遍历两个列表,因此我能够检查它们的输入是否与该问题的答案相同。我坚持要退出第二个循环,以便我可以继续处理其他问题,我曾尝试打破循环,但它中断了并且不会提示用户回答。请问我该如何解决这个问题。下面是我的代码,主程序从 for-loop

开始
package javaapplication19;

import java.util.Scanner;
import java.util.ArrayList;
import java.time.Duration;
import java.time.Instant;

public class trivia {
    public static <string> void main(String[] args) {

        ArrayList<String> question = new ArrayList<String>();

        question.add(
                "\n\n1. The highest mountain of the world is in which two countries \n\tA. India and Pakistan  \n\tB. China and Tibet \n\tC. Tibet and Nepal \n\tD. Pakistan and Nepal \n\tE. Tanzania and India");
        question.add(
                "The lowest point of land on earth is on the border between which two countries? \n\tA. Mexico and the U.S  \n\tB. Holland and Belgium \n\tC. Israel and Jordan \n\tD. Denmark and Germany \n\tE. Sweden and Tunisia");
        question.add(
                "Where is the biggest desert on earth \n\tA. Siberia  \n\tB. Antartica \n\tC. Africa \n\tD. California \n\tE. Pakistan ");
        question.add(
                "Which capital city in the world is at the highest altitude? \n\tA. Bern, Switzerland  \n\tB. Katmandu, Nepal \n\tC. Ulaanbaatar, Mongolia \n\tD. La Paz, Bolivia \n\tE. Lagos, Nigeria");
        question.add(
                "Which continent has the fewest people leiving in it? \n\tA. Africa  \n\tB. Asia\n\tC. Australia \n\tD. Antartica \n\tE. South America");
        question.add(
                "Which continent has the largest people living on it? \n\tA. Africa  \n\tB. Asia \n\tC. Australia \n\tD. Antartica \n\tE. North America");
        question.add(
                "Which continent has the most countries \n\tA. North America  \n\tB. Asia \n\tC. Antartica \n\tD. Australia \n\tE. Africa");
        question.add(
                "Which country is the biggest in Land area \n\tA. Russia  \n\tB. China \n\tC. Canada \n\tD. The United States \n\tE. India");
        question.add(
                "Which ocean does not border North America \n\tA. Pacific  \n\tB. Atlantic \n\tC. Artic \n\tD. Indian \n\tE. Southern");
        question.add(
                "Which country is the densest in population? \n\tA. Monaco  \n\tB. Singapore \n\tC. China \n\tD. Bahrain \n\tE. Mongolia");

        ArrayList<String> answer = new ArrayList<String>();

        answer.add("C");
        answer.add("C");
        answer.add("B");
        answer.add("D");
        answer.add("D");
        answer.add("B");
        answer.add("E");
        answer.add("A");
        answer.add("D");
        answer.add("A");

        System.out.print(
                "Hey there welcome to the Trivia game, here are the rules and Instructions of the game: \n\n\t1. When you run the game, you would be prompted with a question and 5 answer choice, of which 1 is just true. \n\t2. You are to pick just one answer. \n\t3. To submit your answer you press enter. \n\t4. After submitting your answer you would be promted if you want to play again");

        Scanner sc = new Scanner(System.in);

//Main start of the program
        for (int i = 0; i < question.size(); i++) {
            System.out.print(question.get(i));

            for (int j = 0; j < answer.size(); j++) {
                System.out.print("\n");

                long starttime = System.nanoTime();
                String useranswer = sc.nextLine();
                long endtime = System.nanoTime();
                long Duration = endtime - starttime;
                // converting nanoseconds to seconds
                // 1 Second = 1000000000 Nanosecond
                double seconds = (double) Duration / 1000000000;

                if (!answer.get(j).equals(useranswer)) {
                    System.out.println("Aww you got the question wrong :( ");
                    System.out.println("You also took " + seconds + "secs to answer the question");

                    System.out.println("Would you like to play again (yes/no): ");
                    String userinput = sc.next();
                    String input = userinput.toLowerCase();
                    String Validanswer = "yes";

                    if (input.equals(Validanswer)) {
                        System.out.println("\n");
                         break;                     //Where the problem occurs
                    }

                    else {
                        System.out.println("Thanks for playing, see you !");
                        System.exit(j);
                        sc.close();
                    }

                }

                else if (answer.get(j).equals(useranswer)) {
                    System.out.println("You got the Question Right!!");
                    System.out.println("You also took " + seconds + "secs to answer the question");

                    System.out.println("Would you like to play again (yes/no): ");
                    String userinput = sc.next();
                    String input = userinput.toLowerCase();
                    String Validanswer = "yes";

                    if (input.equals(Validanswer)) {
                        System.out.println("\n");
                       break;                        //Also where the problem occurs
                    }

                    else {
                        System.out.println("Thanks for playing, see you !");

                        System.exit(j);
                        sc.close();
                    }
                }
        

            }

        }
    }
}

考虑以下代码。为了简单起见,我删除了定时器,并将其转换为一个没有任何重复代码的简单循环。自己尝试 运行 并记下代码中的更改:

//Main start of the program
for (int i = 0; i < question.size(); i++) {
    //Ask the question
    System.out.println(question.get(i));

    //Get the answer
    String useranswer = sc.nextLine();
    
    //Check the result (note we only need if/else, not if/else if)
    if (!answer.get(i).equals(useranswer)) {
        System.out.println("Aww you got the question wrong :( ");
    }
    else {
        System.out.println("You got the Question Right!!");
    }
    
    //Check if the user wants to keep playing:
    System.out.println("Would you like to keep playing (yes/no): ");
    String userinput = sc.nextLine().toLowerCase();

    //Exit if the user does not type "yes", or just let the loop finish and restart
    if (!input.equals("yes")) {
        System.out.println("Thanks for playing, see you !");
        System.exit(0);
    }
}

正如我们所见,这是通过一个循环完成的。如果用户在回答问题后输入 yes 继续播放,那么循环简单地重复并打印下一个问题 System.out.println(question.get(i));,因此如果用户回答 no,它只是打印消息并退出。