以下代码声称使用具有标志的do循环重复输入,直到获得有效的int。关于此Java代码段的问题
do
{
try
{
// attempt to convert the String to an int
n = Integer.parseInt(s);
goodInput = true;
}
catch (NumberFormatException nfe)
{
s = JOptionPane.showInputDialog(null,
s + " is not an integer. Enter an integer");
}
} while (!goodInput);
我对这里的逻辑有点困惑。如果刚刚的Integer.parseInt正常工作,或没有异常的现象发生,那么“goodInput”被指定为“true”在
goodInput = true;
线接!goodInput将被评估为假,因此而循环将继续。这在我看来与设计逻辑矛盾,即在执行正确的解析操作后while循环应该停止。我上面的分析有什么问题。