Java 答错后重复
今天我在胡闹,我想做一个选择题。我走到这一步,它起作用了。但是我想知道,如果用户的答案错了,我如何让它重复这个问题?如果有人能帮我,那就太好了!谢谢Java 答错后重复,java,Java,今天我在胡闹,我想做一个选择题。我走到这一步,它起作用了。但是我想知道,如果用户的答案错了,我如何让它重复这个问题?如果有人能帮我,那就太好了!谢谢 import java.util.Scanner; public class multipleChoiceTest { public static void main(String[] args) { Scanner myScanner = new Scanner(System.in); System.o
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!");
(记住,在他上次出错后,我们需要重新输入!)在这种情况下,您可以使用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的书籍。@GGrec在java中不起作用,它必须是
while(true)
。使用循环;与其在堆栈溢出上发布这个问题,不如在java上阅读教程,或者在java上读一本书。@ ggRC++不在Java中工作,它必须是“代码”>(真) >代码(1)< /C>应该是“代码>”(true) @ SiMunRealFordsBurg OOP,该死的C++。<代码>(1)应该是<代码>(真)<代码> @西蒙德安德雷斯伯格OOPS,该死的C++。