gpt4 book ai didi

java - 创建重新检查 if 语句

转载 作者:行者123 更新时间:2023-12-01 10:03:25 26 4
gpt4 key购买 nike

我正在为类(class)编写平均成绩脚本,但我不知道如何重复交叉检查部分,直到获得有效值(代码在下面列出)。运行时,如果原始输入无效,它允许我进行更正,但只执行一次。我怎样才能循环直到得到正确的响应?提前致谢!

  if (grade1 >= 0 && grade1 <= 100)
System.out.println("First grade valid.\n");
else
{
System.out.println("Grade invalid. Re-enter first grade:");
grade1 = kb.nextDouble();
}

最佳答案

考虑

grade = -1;
System.out.println("Enter a grade:\n");
grade1 = kb.nextDouble();
if (grade1 >= 0 && grade1 <= 100)
{
System.out.println("First grade valid.\n");
}
else
{
while (grade1 < 0 || grade1 > 100)
{
System.out.println("Grade invalid. Re-enter first grade:");
grade1 = kb.nextDouble();
}
}

关于java - 创建重新检查 if 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36633463/

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