我正在为我的班级做一个平均成绩脚本,在我得到一个有效值之前,我无法弄清楚如何重复进行交叉检查部分(下面列出了代码)。运行后,如果原始输入无效,它可以让我进行更正,但仅进行一次。我如何循环播放,直到获得正确的答复?提前致谢!
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();
}
}