public static String getQtyInput(String prompt) throws Exception
{
String qtyValue;
int counter = 0;
do
{
qtyValue = getStringInput(prompt);
counter++;
} while (counter < 3);
if (Integer.parseInt(qtyValue) > 1 || Integer.parseInt(qtyValue) < 99)
{
throw new Exception("Invalid input! \n Must enter 1-99");
}
return qtyValue;
}
最佳答案
考虑一下您要检查的内容。 1 到 99 范围内的数字是正确的,因此您只想在数字超出该范围时显示异常。换句话说,当它低于 (<) 低于 1 或高于 (>) 高于 99 时。
因此您需要将 if 语句重写为:
if (Integer.parseInt(qtyValue) < 1 || Integer.parseInt(qtyValue) > 99)
关于java - 当我输入正确的值 1-99 时,我仍然遇到异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35006217/