繁体   English   中英

我怎样才能让我的程序接受超过9位的数字?

[英]how can i get my program to accept more than 9 digit numbers?

我目前正在编写一个测试素数的小应用程序。 这是基于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());

        }
    }

}

假设Algorithim类是一个自定义的书面类,则可以使用BigInteger替换其构造函数中的integer参数以容纳更大的值。

您将像这样更新jlSolution字段:

Algorithm algorithm = new Algorithm(new BigInteger(jtfX.getText()));
jlSolution.setText(algorithm.check()); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM