在“请重新启动并再试一次”后,我试图让程序循环开始“你好,你叫什么名字?”
import java.util.Scanner;
public class Learning {
public static void main(String[] args)
{
System.out.println("Hello, What is your name?");
Scanner scanner = new Scanner(System.in);
String yourName = scanner.nextLine();
System.out.println("Is: " +yourName + " your name?");
Scanner scanner1 = new Scanner(System.in);
String isCorrect = scanner1.nextLine();
if (isCorrect.equals("Yes"))
{
System.out.println("Thank you, Please proceed with the quiz!");
}
else
{
System.out.println("Please restart and try again.");
System.exit(0);
}
答案 0 :(得分:1)
只需使用do-while循环并删除System.exit(0);
do {
System.out.println("Hello, What is your name?");
Scanner scanner = new Scanner(System.in);
String yourName = scanner.nextLine();
System.out.println("Is: " +yourName + " your name?");
Scanner scanner1 = new Scanner(System.in);
String isCorrect = scanner1.nextLine();
if (isCorrect.equals("Yes"))
{
System.out.println("Thank you, Please proceed with the quiz!");
}
else
{
System.out.println("Please restart and try again.");
}
} while (!isCorrect.equals("Yes"));
答案 1 :(得分:1)
我添加了一段时间声明:
while(isCorrect.equals("noValue")&&(!isCorrect.equals("yes")||!isCorrect.equals("yes"))) {
所以它一直持续到isCorrect不等于“noValue”并且它等于“是”或“是” 这是整个代码:
import java.util.Scanner;
public class Learning {
public static void main(String[] args) {
String isCorrect = "noValue";
//If isCorrect equals noValue, keep going until it equals yes or Yes
while(isCorrect.equals("noValue")&&(!isCorrect.equals("yes")||!isCorrect.equals("Yes"))) {
System.out.println("Hello, What is your name?");
//You don't really need two scanners if they are both for the same thing
Scanner scanner = new Scanner(System.in);
String yourName = scanner.nextLine();
System.out.println("Is: " +yourName + " your name?");
isCorrect = scanner.nextLine();
//include the if and else statements so the program knows what to say
//and if it should repeat or not
if(isCorrect.equals("yes")||isCorrect.equals("Yes")) {
System.out.println("Thank you, please proceed with the quiz!");
} else {
System.out.println("Please try again");
isCorrect = "noValue";
}
}
//Continue with the quiz here!
}
}
我希望我帮忙!