如何突破第二个循环(嵌套)并继续循环



我的Java程序需要帮助。我是一个初学者,所以这对我来说真的很困惑。我正在尝试建立一个多问题的Trivia程序。提示用户提出问题,用户必须从给出的答案中进行选择。如果他们答对了或答错了,他们会被告知,并显示他们回答问题所花费的时间。现在的问题是,在他们提交了一个问题的答案后,如果他们想再次比赛,就会出现另一个问题。我有两个列表1用于问题,1用于答案。我正在两个列表中同时迭代2个用于循环,这样我就可以检查他们的输入是否与该问题的答案相同。我一直在跳出第二个循环,这样我就可以继续其他问题了,我试过打破这个循环,但它打破了,没有提示用户回答。我该怎么解决这个问题。下面是我的代码,主程序从for循环开始

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(
"nn1. The highest mountain of the world is in which two countries ntA. India and Pakistan  ntB. China and Tibet ntC. Tibet and Nepal ntD. Pakistan and Nepal ntE. Tanzania and India");
question.add(
"The lowest point of land on earth is on the border between which two countries? ntA. Mexico and the U.S  ntB. Holland and Belgium ntC. Israel and Jordan ntD. Denmark and Germany ntE. Sweden and Tunisia");
question.add(
"Where is the biggest desert on earth ntA. Siberia  ntB. Antartica ntC. Africa ntD. California ntE. Pakistan ");
question.add(
"Which capital city in the world is at the highest altitude? ntA. Bern, Switzerland  ntB. Katmandu, Nepal ntC. Ulaanbaatar, Mongolia ntD. La Paz, Bolivia ntE. Lagos, Nigeria");
question.add(
"Which continent has the fewest people leiving in it? ntA. Africa  ntB. AsiantC. Australia ntD. Antartica ntE. South America");
question.add(
"Which continent has the largest people living on it? ntA. Africa  ntB. Asia ntC. Australia ntD. Antartica ntE. North America");
question.add(
"Which continent has the most countries ntA. North America  ntB. Asia ntC. Antartica ntD. Australia ntE. Africa");
question.add(
"Which country is the biggest in Land area ntA. Russia  ntB. China ntC. Canada ntD. The United States ntE. India");
question.add(
"Which ocean does not border North America ntA. Pacific  ntB. Atlantic ntC. Artic ntD. Indian ntE. Southern");
question.add(
"Which country is the densest in population? ntA. Monaco  ntB. Singapore ntC. China ntD. Bahrain ntE. 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: nnt1. When you run the game, you would be prompted with a question and 5 answer choice, of which 1 is just true. nt2. You are to pick just one answer. nt3. To submit your answer you press enter. nt4. 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,则简单地打印消息并退出。

最新更新