簡體   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