java - 如何捕获字母并显示错误(仅整数除外)?

原文 标签 java bluej

对编码来说还很陌生,我只想接受整数并显示一条JOptionPane警告消息,即仅允许整数。我尝试使用NumberFormatException,但是我对这种方法不熟悉。谢谢!

    import javax.swing.JOptionPane;
import java.util.Random;
public class GuessingGame
{
    public static void main(String[] args)
    {
        int start = JOptionPane.showConfirmDialog(null, "You must guess a number between 100 and 500 in a set amount of guesses.", "Press 'Yes' to start the guessing game.", JOptionPane.YES_NO_OPTION); 
        if(start == JOptionPane.YES_OPTION)
        {
            Random rng = new Random();
            int theNumber = rng.nextInt((500-100)+1)+100; 
            String guess;
            int maxAttempt = 1;
            int inputNumber;
            System.out.println(theNumber);
            while(maxAttempt<=4)
            {
                guess = JOptionPane.showInputDialog(null, "Please input a number between 100-500 you have four chances.");
                inputNumber = Integer.parseInt(guess);
                try
            {
            }catch(NumberFormatException e){
                JOptionPane.showMessageDialog(null, "You must only enter an integer", "Guessing Game", JOptionPane.WARNING_MESSAGE);
            }
                if(inputNumber==theNumber)
                {
                    JOptionPane.showMessageDialog(null, "Winner! Guess is correct.", "Guessing Game", JOptionPane.INFORMATION_MESSAGE);
                    break;
                }
                else if(inputNumber>theNumber)
                    JOptionPane.showMessageDialog(null, "Guess was greater than the real number.", "Guessing Game", JOptionPane.WARNING_MESSAGE);
                else
                    JOptionPane.showMessageDialog(null, "Guess was less than the real number.", "Guessing Game", JOptionPane.WARNING_MESSAGE);
                maxAttempt++;
            }

            if(maxAttempt>4)
                JOptionPane.showMessageDialog(null, "Game Over. You have exceeded your guessing limit.", "Guessing Game", JOptionPane.ERROR_MESSAGE);
        }
        else
            JOptionPane.showMessageDialog(null, "Quiting Game, press 'Ok' to exit.", "Guessing Game", JOptionPane.INFORMATION_MESSAGE);




    }
}

最佳答案

您需要做的是在try-catch内部调用Integer#parseInt。然后,当发生异常时,您需要继续进行while循环的下一次迭代。

更换

                inputNumber = Integer.parseInt(guess);
                try  {
                }catch(NumberFormatException e){
                    JOptionPane.showMessageDialog(null, "You must only enter an integer", "Guessing Game", JOptionPane.WARNING_MESSAGE);
                }




                try {
                    inputNumber = Integer.parseInt(guess);
                } catch (NumberFormatException e) {
                    JOptionPane.showMessageDialog(null, "You must only enter an integer", "Guessing Game", JOptionPane.WARNING_MESSAGE);
                    continue;
                }

关于java - 如何捕获字母并显示错误(仅整数除外)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60591634/

相关文章:

java - 如何在不提示用户发生异常的情况下处理异常并完成方法的功能

java - 在JAVA中使用字符串的回文

java - IDE没有正确的类路径

java - Gridlayout 按钮单击然后在按钮下加载一个表单 Java

java - 快速排序数字(字符串)

java - 如何在Java中的<>之间获取字符串

JavaFX 使对话框像 native Windows 对话框一样闪烁

java - 计算赢/输、赢率和总赢款

java - 减少功耗,同时应使用无线

java - 将 Hibernate 查询的结果映射到对象