gpt4 book ai didi

java - 错误答案后重复

转载 作者:行者123 更新时间:2023-11-30 07:13:50 25 4
gpt4 key购买 nike

今天我在胡闹,我试图创建一个多项选择测试。我做到了这一点并且它有效。不过我想知道,如果用户答错了,我该如何让它重复这个问题?如果有人能帮助我,那就太棒了!谢谢!

import java.util.Scanner;
public class multipleChoiceTest {

public static void main(String[] args) {
Scanner myScanner = new Scanner(System.in);

System.out.println("What color is the sky?");
System.out.println("A. Blue");
System.out.println("B. Green");
System.out.println("C. Yellow");
System.out.println("D. Red");
String userChoice = myScanner.nextLine();

if (userChoice.equalsIgnoreCase("a")) {
System.out.println("You're right!");
} else {
System.out.println("You're wrong! Try Again.");

}
}

最佳答案

在这种情况下,您可以使用 While 语句!我们这样看:只要用户没有回答正确,你就不会继续。现在将“只要”更改为“while(...)”我们将得到这段代码:

Scanner myScanner = new Scanner(System.in);
System.out.println("What color is the sky?");
System.out.println("A. Blue");
System.out.println("B. Green");
System.out.println("C. Yellow");
System.out.println("D. Red");
String userChoice = myScanner.nextLine();

while(! userChoice.equalsIgnoreCase("a")){
System.out.println("You're wrong! Try Again.");
userChoice = myScanner.nextLine();
}
System.out.println("You're right!");

(记住,在他上次输入错误后,我们需要重新输入!)

关于java - 错误答案后重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19082920/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com