gpt4 book ai didi

java - 当我输入正确的值 1-99 时,我仍然遇到异常

转载 作者:行者123 更新时间:2023-12-01 21:52:14 25 4
gpt4 key购买 nike

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/

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