gpt4 book ai didi

java - 如果用户输入错误答案,如何跳回到 if 语句的开头

转载 作者:行者123 更新时间:2023-11-30 05:52:03 25 4
gpt4 key购买 nike

代码如下:

if (g1 == 6) {
System.out.println("Correct! Proceed to the next challenge");
}
else {
System.out.println("That is incorrect, please try again");
}

如果他们输入“4”作为他们的猜测,我如何允许他们重试?基本上,我如何返回到 if 语句的开头?

最佳答案

String g1 = "";
try {
InputStreamReader converter = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(converter);
do {
g1 = in.readLine();
if (g1 == 6) {
System.out.println("Correct! Proceed to the next challenge");
} else {
System.out.println("That is incorrect, please try again");
}
} while(g1 != 6);
} catch (Exception e) {
System.out.println("Error! Exception: "+e);
}

关于java - 如果用户输入错误答案,如何跳回到 if 语句的开头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12099196/

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