猿问

如何在我的代码中添加 try 、 catch 语句以仅接收整数值?

我创建了一个 JOptionPane 对话框来接受用户的输入以选择他们想要购买的蛋糕类型,但我只想接受一个整数值。我对 java 编程很陌生,需要一些帮助,使用 try, catch 只获取一个整数值。


我创建了“Cakes[]”数组来存储和检索蛋糕的味道、蛋糕的价格和剩余的蛋糕数量。


do {


    do {

        String userinput = JOptionPane.showInputDialog(null, "Enter your choice of cake:"

        + "\n" + "1." + Cakes[0].getflavorofcake() + "(" + Cakes[0].getpriceofcake() + "cents" + ")" + "    no. of cakes available:" + Cakes[0].getnofcaksleft()

        + "\n" + "2." + Cakes[1].getflavorofcake() + "(" + Cakes[1].getpriceofcake() + "cents" + ")" + "      no. of cakes available:" + Cakes[1].getnofcaksleft()

        + "\n" + "3." + Cakes[2].getflavorofcake() + "(" + Cakes[2].getpriceofcake() + "cents" + ")" + "            no. of cakes available:" + Cakes[2].getnofcaksleft()

        + "\n" + "4." + Cakes[3].getflavorofcake() + "(" + Cakes[3].getpriceofcake() + "cents" + ")" + "        no. of cakes available:" + Cakes[3].getnofcaksleft()

        + "\n" + "5." + Cakes[4].getflavorofcake() + "(" + Cakes[4].getpriceofcake() + "cents" + ")" + "          no. of cakes available:" + Cakes[4].getnofcaksleft(), "mini cake shop", JOptionPane.QUESTION_MESSAGE);


        choiceofcake = Integer.parseInt(userinput);


        showthatthereisnocakesleft(choiceofcake);//Method called to show user that the choiceofcake chosen is no longer available


    } while (Cakes[choiceofcake - 1].getnofcaksleft() < 1);


    if (choiceofcake > 5 || choiceofcake < 1) {

    JOptionPane.showMessageDialog(null, "Invalid input! Please enter in the range from  1 to 5", "Error", JOptionPane.ERROR_MESSAGE);

    }

} while (choiceofcake > 5 || choiceofcake < 1);


一只甜甜圈
浏览 148回答 3
3回答

慕尼黑8549860

包裹choiceofcake = Integer.parseInt(userinput);尝试捕捉try {&nbsp; &nbsp;choiceofcake = Integer.parseInt(userinput);&nbsp; &nbsp;if (choiceofcake > 5 || choiceofcake < 1) {&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp;}} catch (NumberFormatException ee) {&nbsp; &nbsp; ee.printStatckTrace ();&nbsp; &nbsp; continue;}

浮云间

由于您是 Java 新手,因此 try-catch 的想法是在可能发生异常的情况下包围一段代码。在您的情况下,如果“userInput”无法转换为数字,“parseInt”可能会引发异常。可以参考官方文档发现哪些异常类型:&nbsp;https://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#parseInt(java.lang.String)您还可以创建和/或抛出您自己的异常。例如,在这种情况下,您可以在检查“userInput”是否超出所需值后引发 IllegalArgumentException:throw&nbsp;new&nbsp;IllegalArgumentException();您可以在某处捕获异常,然后显示带有警报的消息对话框。

呼啦一阵风

假设您希望循环继续,即使在输入错误的情况下,您也可以尝试以下操作:do {&nbsp; &nbsp; String userinput = JOptionPane.showInputDialog(null, "Enter your choice of cake:" +&nbsp; &nbsp; &nbsp; &nbsp; "\n" + "1." + Cakes[0].getflavorofcake() + "(" + Cakes[0].getpriceofcake() + "cents" + ")" + "&nbsp; &nbsp; no. of cakes available:" + Cakes[0].getnofcaksleft() +&nbsp; &nbsp; &nbsp; &nbsp; "\n" + "2." + Cakes[1].getflavorofcake() + "(" + Cakes[1].getpriceofcake() + "cents" + ")" + "&nbsp; &nbsp; &nbsp; no. of cakes available:" + Cakes[1].getnofcaksleft() +&nbsp; &nbsp; &nbsp; &nbsp; "\n" + "3." + Cakes[2].getflavorofcake() + "(" + Cakes[2].getpriceofcake() + "cents" + ")" + "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; no. of cakes available:" + Cakes[2].getnofcaksleft() +&nbsp; &nbsp; &nbsp; &nbsp; "\n" + "4." + Cakes[3].getflavorofcake() + "(" + Cakes[3].getpriceofcake() + "cents" + ")" + "&nbsp; &nbsp; &nbsp; &nbsp; no. of cakes available:" + Cakes[3].getnofcaksleft() +&nbsp; &nbsp; &nbsp; &nbsp; "\n" + "5." + Cakes[4].getflavorofcake() + "(" + Cakes[4].getpriceofcake() + "cents" + ")" + "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; no. of cakes available:" + Cakes[4].getnofcaksleft(), "mini cake shop", JOptionPane.QUESTION_MESSAGE);&nbsp; &nbsp; choiceofcake = Integer.parseInt(userinput);&nbsp; &nbsp; if (choiceofcake > 5 || choiceofcake < 1) {&nbsp; &nbsp; &nbsp; &nbsp; JOptionPane.showMessageDialog(null, "Invalid input! Please enter in the range from&nbsp; 1 to 5", "Error", JOptionPane.ERROR_MESSAGE);&nbsp; &nbsp; }&nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; showthatthereisnocakesleft(choiceofcake);&nbsp; &nbsp; }} while (Cakes[choiceofcake - 1].getnofcaksleft() < 1);如果您希望循环在输入错误时完全停止,请考虑IllegalArgumentException在发生这种情况时抛出一个。
随时随地看视频慕课网APP

相关分类

Java
我要回答