2

所以我是java新手,我正在尝试使用try and catch。例如,如果我问用户有多少葡萄,并且他们输入一串串字母,它将显示一个错误对话框,而不是仅仅得到一个系统错误。我可以用扫描仪做到这一点,但不能用 JOptionPane。我真的希望出现一个对话框,这就是我尝试使用 JOptionPane.showInputDialog 的原因。

扫描仪可以工作...=

import java.util.Scanner;

class test {
    public static void main (String[] args)
    {

        Scanner input = new Scanner(System.in);

        System.out.println("How many grapes do u have?");
        int grapes = 1;
        try
        {
            grapes = input.nextInt();
        }
        catch (Exception e)
        {
            System.out.println("Good job Sherlock you broke the program");
            return;
        }
        int mg;

        if (grapes >= 100)
            mg = 1;

        else
            mg = 2;

        switch (mg){
            case 1: System.out.println("You got a lot of grapes");
                break;
            case 2: System.out.println("You brarely got any grapes");
                break;
        }
    }
}

JOptionPane 不起作用...

import javax.swing.JOptionPane;

public class bday
{
    public static void main(String[] args)
    {
        String age = "0";
        try
        {
            age = JOptionPane.showInputDialog("What was your age yesterday?");
        }
        catch(Exception e)
        {
            JOptionPane.showMessageDialog(null, "Thanks a lot, you broke it. CYA later.");
            return;
        }
        int iage = Integer.parseInt(age);

        String bday = "0";

         try
         {
             bday = JOptionPane.showInputDialog("Was yesterday your B-Day? (True or False)");
         }
         catch (Exception e)
         {
            JOptionPane.showMessageDialog(null, "WHY U MESS UP PROGRAM???.... BYE BYE!!");
            return;
         }
         boolean bage = Boolean.parseBoolean(bday);
        if (bage == true){
            iage += 1;
            JOptionPane.showMessageDialog(null, "You are now " + iage);
        }
        else if (bage == false){
            JOptionPane.showMessageDialog(null, "Happy unbirthday!");
        }
        if (iage ==10){
            JOptionPane.showMessageDialog(null, "Congrats, double digits!");
        }
        if (iage >19){
            JOptionPane.showMessageDialog(null,  "U aint a Teenager");
        }
        else if (iage < 13)
            JOptionPane.showMessageDialog(null,  "U aint a Teenager");          

    }
}
4

2 回答 2

1

你在尝试/抓住错误的东西。您应该将解析语句放在 try 块中,因为这是引发异常的原因。

例如,不

    String age = "0";
    try
    {
        age = JOptionPane.showInputDialog("What was your age yesterday?");
    }
    catch(Exception e)
    {
        JOptionPane.showMessageDialog(null, "Thanks a lot, you broke it. CYA later.");
        return;
    }
    int iage = Integer.parseInt(age);

反而:

String age = JOptionPane.showInputDialog("What was your age yesterday?");
try {
  iage = Integer.parseInt(age);
} catch (NumberFormatException nfe) {
  // show error
}  

此外,您应该避免捕获Exception,而应该只捕获特定的异常,这里NumberFormatException


编辑
在您询问的评论中:

还有一个问题,我应该对布尔值做同样的事情吗?

在解析方面,布尔类型有点棘手(在我看来)。要了解其Boolean.parseBoolean(...)工作原理,请查看Boolean API,特别是 parseBoolean 方法。如果输入的文本没有意义,您会看到它不会抛出 NumberFormatException。API 会告诉你实际上返回了什么。try/catch 块在这里不起作用。equalsIgnoreCase(...)如果您需要捕获错误,请考虑使用字符串。

于 2013-07-18T23:50:23.000 回答
1

查看失败的行。我怀疑是boolean bage = Boolean.parseBoolean(bday);。您可以自由地将任何内容保存在字符串中,但是将“whatever”转换为布尔值可能会失败。

把它放在你的 try/catch 中。

于 2013-07-18T23:52:25.773 回答