0
        ans = JOptionPane.showInputDialog(null,"There are currently "+clubSize+" people inside right now" +
                                                            "\nHow many People are in your party today.");
        int partyIn;
        try
            {
           partyIn = Integer.parseInt(ans);
            }
        catch (NumberFormatException e)
            {
           JOptionPane.showMessageDialog(null, "What you entered was not a number: " + ans);
            }
        if (clubSize + partyIn <= 125)
            {
            clubSize = clubSize + partyIn; 
            peopleIn = peopleIn + partyIn;
            }
        else
            {
            JOptionPane.showMessageDialog(null, "Sorry you have to many people in your party");
            }

这将返回一个错误:变量 partyIn 可能尚未初始化

4

1 回答 1

1

如果输入的数字不是实数,则使用Integer.parseInt将抛出 a的事实。NumberFormatException捕获该异常,然后将错误通知用户。

int partyIn;
try
{
   partyIn = Integer.parseInt(ans);
}
catch (NumberFormatException e)
{
   JOptionPane.showMessageDialog(null, "What you entered was not a number: " + ans);
}
于 2013-02-28T19:57:00.743 回答