【发布时间】:2016-05-24 23:22:59
【问题描述】:
所以我正在开发一个程序,允许用户将学生添加到班级以及管理他们的成绩等等。当用户选择菜单中的第一个选项时,他必须输入一个 id(强制),但他也可以添加一个数字分数和/或字母等级。根据另一篇文章中的反馈,我设法创建了一个读取用户输入的字符串变量行,然后检查它是否是“S”/“s”(是否跳过)并相应地将值解析为 double。现在基于这个问题,如果用户决定跳过添加分数,我如何跳过提示并继续下一个提示?我尝试使用 break;但它会退出整个循环。有没有办法跳过分数问题并继续进行字母等级问题?
输出:
1) 将学生添加到班级
2)从班级中删除学生
3) 为学生设置成绩
4) 编辑学生的成绩
5) 显示课堂报告
6) 退出
1
请输入id: 请输入分数:(输入 s 跳过)
请输入成绩:(输入 s 跳过)
代码
// Prompting the user for Score (Numerical Grade)
System.out.println("Kindly input Score: (Enter s to Skip)");
// reading the input into the line variable of string datatype
String line = input.nextLine();
// checking if line =="s" or =="S" to skip, otherwise
// the value is parsed into a double
if("s".equals(line) || "S".equals(line))
{
break; // this exists the loop. How can I just skip this requirement
//and go to the next prompt?
}else try
{
score = Double.parseDouble(line);
System.out.println(score);
} catch( NumberFormatException nfe)
{
}
// Prompting the user for Numerical Grade
System.out.println("Kindly input Grade: (Enter s to Skip)");
String line2 = input.nextLine();
if("s".equals(line2) || "S".equals(line2))
{
break; // this exists the loop. How can I just skip this
// requirement and go to the next prompt?
}else try
{
score = Double.parseDouble(line2);
System.out.println(score);
} catch( NumberFormatException nfe)
{
}
【问题讨论】: