2

我目前正在编写一个测试素数的小应用程序。它是基于 gui 的,但我遇到了一个问题。我在程序中添加了一些限制,用户只能使用 numberformatexception 处理程序输入数字,但每当用户输入超过 9 位长度的数字时,它不再将其视为数字。这个问题有解决方案吗?我在下面留下了我的代码。

static void validation() // This is what happens when the "Check" button is clicked
{


    // Retrieve information from the fields and print it out on the Frame
    if (jtfX.getText().trim().length() == 0) // Check if the field is empty
    {
        jlSolution.setText("You have not entered anything yet");

    }

    else // Otherwise...
    {

        try // In general....
        {

            if (Long.parseLong(jtfX.getText()) < 0) // Check if it is a negative value
            {
                                jlSolution.setText("The number you entered is less than zero");
            }
                            else // If it isn't...
            {
                                jlSolution.setText(new Algorithm(Integer.parseInt(jtfX.getText())).check()); // ....then check if this number is prime.
            }
        }

        catch (NumberFormatException nfe) // ... always catch those who refuse to follow simple rules!
        {
            jlSolution.setText("Numerical values only please. " + "You entered: " + jtfX.getText());

        }
    }

}
4

1 回答 1

1

假设该类Algorithim是自定义编写的类,您可以将其构造函数中的整数参数替换为 aBigInteger以保存更大的值。

您将像这样更新您的jlSolution字段:

Algorithm algorithm = new Algorithm(new BigInteger(jtfX.getText()));
jlSolution.setText(algorithm.check()); 
于 2012-10-27T15:58:22.880 回答