如何在我的代码中添加try,catch语句以只接受整数值?

zkure5ic  于 2021-07-09  发布在  Java
关注(0)|答案(4)|浏览(360)

我正在创建一个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);
c3frrgcw

c3frrgcw1#

您可以添加try catch语句,这样如果用户输入了错误的值,您就可以显示他们的错误,而不是终止程序:

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);

            try{
                    choiceofcake = Integer.parseInt(userinput);
                    if (choiceofcake > 5 || choiceofcake < 1) {
                        JOptionPane.showMessageDialog(null, "Invalid input! Please enter in the range from  1 to 5", "Error", JOptionPane.ERROR_MESSAGE);
                    }
                    else {
                        showthatthereisnocakesleft(choiceofcake);
                    }
            }catch(NumberFormatException e){
                // e.printStackTrace();
            }catch(Exception e){
                // e.printStackTrace();
            }

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

zsohkypk2#

假设希望循环继续,即使在输入错误的情况下,也可以尝试以下操作:

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);

    if (choiceofcake > 5 || choiceofcake < 1) {
        JOptionPane.showMessageDialog(null, "Invalid input! Please enter in the range from  1 to 5", "Error", JOptionPane.ERROR_MESSAGE);
    }
    else {
        showthatthereisnocakesleft(choiceofcake);
    }
} while (Cakes[choiceofcake - 1].getnofcaksleft() < 1);

如果希望循环在输入错误时完全停止,那么可以考虑抛出 IllegalArgumentException 当这种情况发生时。

kx1ctssn

kx1ctssn3#

Package

choiceofcake = Integer.parseInt(userinput);

试着接球

try {
   choiceofcake = Integer.parseInt(userinput);
   if (choiceofcake > 5 || choiceofcake < 1) {
        break;
   }
} catch (NumberFormatException ee) {
    ee.printStatckTrace ();
    continue;
}
6mzjoqzu

6mzjoqzu4#

由于您是java新手,try-catch的思想是在可能发生异常的情况下围绕一段代码。在您的例子中,如果“userinput”不能转换为数字,“parseint”会引发异常。您可以参考官方文档来了解哪些类型的异常:https://docs.oracle.com/javase/7/docs/api/java/lang/integer.html#parseint(java.lang.string)
您还可以创建和/或抛出自己的异常。例如,在本例中,您可以在检查“userinput”是否超出所需值之后引发illegalargumentexception,方法是:

throw new IllegalArgumentException();

您可以在某个地方捕获异常,然后显示带有警报的消息对话框。

相关问题