gpt4 book ai didi

java - 如何让我的脚本循环?

转载 作者:行者123 更新时间:2023-11-30 04:01:32 25 4
gpt4 key购买 nike

在“请重新启动并重试”之后,我试图让程序循环开始到“你好,你叫什么名字?”

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);
}

最佳答案

我添加了一个 while 语句:

while(isCorrect.equals("noValue")&&(!isCorrect.equals("yes")||!isCorrect.equals("yes"))) {

所以它一直持续到 isCorrect 不等于“noValue”并且等于“yes”或“Yes”完整代码如下:

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!
}
}

希望对您有帮助!

关于java - 如何让我的脚本循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21894584/

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