gpt4 book ai didi

java - 验证,如果无效则通知,返回循环

转载 作者:太空宇宙 更新时间:2023-11-04 13:41:46 25 4
gpt4 key购买 nike

我的这部分代码遇到问题。该程序要求输入一些信息(姓名、ID、年级等),然后打印结果。

我决定脱离教程,现在已经把头撞到了众所周知的墙上 -

我想要的伪代码:

Ask user for grade between 9 and 12
If input is less than 9 or greater than 12, return failed message and -return to loop-
If input acceptable, continue to next question.

当前代码如下:

do {
System.out.print("Grade (9-12): ");
while (!keyboard.hasNextInt()) {
System.out.printf("message saying you're wrong");
keyboard.next();
}
userGrade = keyboard.nextInt();
} while (userGrade >= 9 || userGrade <= 12);

最佳答案

尝试这样的事情:

boolean correct = true;
do {
System.out.print("Grade (9-12): ");
userGrade = keyboard.nextInt();
if (userGrade < 9 || userGrade > 12) {
correct = false;
System.out.println("message saying you're wrong");
} else {
correct = true;
}
} while (!correct);

关于java - 验证,如果无效则通知,返回循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31184199/

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