有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java try捕获、循环和信息显示失败

当我输入一个介于-2147483648和2147483647之间的值时,程序关闭,不会像我写的那样显示有效数字。如果我输入一个超出范围的数字,它应该输入一个while循环,直到我在范围之间输入一个有效的数字。然而,当我输入一个超出范围的数字时,它只会显示一个异常错误,这就是为什么我将捕获放在第一位的原因

我已经试过这个问题几个小时了,我还是比较新的编码(第二类),所以我很抱歉,如果这已经得到了回答之前。我查阅了许多旧的答案,并试图将其作为代码的模型。然而,这是我所能做到的

import javax.swing.JOptionPane;

public class test
{
    public static void main (String[] args) throws NumberFormatException
    {
        String input;
        boolean x;
        int number;

        while (x = false)
        {
            try
            {
                input = JOptionPane.showInputDialog("Enter an integer: ");      //creates input box for user to enter integer value
                number = Integer.parseInt(input);

                if ( number < -2147483648 && number > 2147483647)

                    x = false;

                else
                {
                    x = true;
                    System.out.println("You entered: " + number);
                }

                break;
            }

            catch (NumberFormatException e)
            {
                continue;
            }
       }

   }

}

共 (3) 个答案

  1. # 1 楼答案

    一个数怎么能同时小于负数和大于正数

    if ( number < -2147483648 && number > 2147483647)
    

    将其更改为||

    我还建议不要使用magic numbers,而是使用Integer.MIN_VALUEInteger.MAX_VALUE

    另外,当您捕获NumberFormatException时,您不应该将其声明为从main抛出

  2. # 2 楼答案

    您的if条件永远无法计算为true。将while更改为

    while (x == false)
    

    正如其他人所建议的,您还需要将if语句从AND更改为OR

    if ( number < -2147483648 || number > 2147483647)
    

    另外,在else块之后不需要break语句。因为您正在将x设置为true,所以无论如何您都会跳出循环。按照现在的方式,您在第一次迭代中就打破了循环

    此外,您应该将boolean x初始化为false。在顶部,您应该有:

    boolean x = false;你有boolean x;

    因此,当所有这些都说了和做了:

    import javax.swing.JOptionPane;
    
    public class test
    {
        public static void main (String[] args) throws NumberFormatException
        {
            String input;
            boolean x = false;
            int number;
    
            while (x == false)
            {
                try
                {
                    input = JOptionPane.showInputDialog("Enter an integer: ");      //creates input box for user to enter integer value
                    number = Integer.parseInt(input);
    
                    if ( number < -2147483648 || number > 2147483647)
                    {
                        x = false;
                    }
                    else
                    {
                        x = true;
                        System.out.println("You entered: " + number);
                    }
                }
                catch (NumberFormatException e)
                {
                    continue;
                }
           }
       }
    }
    

    进一步清理。当数字超出所需范围时,parseInt抛出异常,我们将进入catch块。因此,您真正需要的是:

    import javax.swing.JOptionPane;
    
    public class test
    {
        public static void main (String[] args) throws NumberFormatException
        {
            String input;
            int number;
    
            while (true)
            {
                try
                {
                    input = JOptionPane.showInputDialog("Enter an integer: ");      //creates input box for user to enter integer value
                    number = Integer.parseInt(input);
    
                    System.out.println("You entered: " + number);
                    break;
                }
                catch (NumberFormatException e)
                {
                    // do nothing
                }
           }
       }
    }
    
  3. # 3 楼答案

    首先我做了这个

     while (x = false)    =>
     while (x == false)
    

    然后Eclipse提醒boolean x未初始化。正如其他一些人指出的,您可以使用其他方法,如while(!x),但这就是它失败的原因

    然后,if(number<;-2147483648&;&;&;number>;2147483647)将始终为false,因为它是int,并且比较是反向的

    以下是修复后的完整代码:

    import javax.swing.JOptionPane;
    
    public class MyTextPanel
    {
        public static void main (String[] args) throws NumberFormatException
        {
            String input;
            long number;
    
            while (true) {
                try {
                    input = JOptionPane.showInputDialog("Enter an integer: ");      //creates input box for user to enter integer value
                    number = Long.parseLong(input);
    
                    if ( number < -2147483648 || number > 2147483647)
                        continue;
    
                    else  {
                        System.out.println("You entered: " + number);
                        break;
                    }
                } catch (NumberFormatException e)  {
                    continue;
                }
           }
       }
    }