0

プログラムで double が正しく検証されることを確認するのが困難です。ユーザーはアカウントに入金する金額を入力できますが、これは 2 倍にする必要があります (私が使用すべきものではないことはわかっていますが、割り当てガイドラインの一部です)。理論的には、ユーザーは任意の金額 (£30 だけでなく、£15.23 など) を入金できるはずです。これは私が現在持っている検証であり、数字は許可されますが、ピリオドの入力は妨げられ、多くの問題が発生します。

これが私がこれまでに持っているコードです:

public static String getBalanceValidation()
{
    //Allow user input capabilities
    Scanner input = new Scanner (System.in);
    //Declare variables needed for validation
    double dblInput = 0; //dblInput is set as 0
    String strNumber = ""; //strNumber is blank
    boolean bolSuccessful, bolNumeric;
    int intCount;
    char charLetter;


    do
    {
        //set bolSuccessful and bolNumeric as true
        bolSuccessful = true;
        bolNumeric = true;

        try //try user input
            {
                System.out.println("Enter the balance to be deposited: "); //User prompt
                strNumber = input.next(); //User input as string
                dblInput = Double.parseDouble(strNumber) ; //String input converted to double


            }// end of try

        catch (NumberFormatException e) //NumberFormatException disallows letters or symbols in value
            {
                System.out.println("Deposit value cannot contain letters!"); //Error message
                bolSuccessful = false; //set bolSuccessful as false

                continue; //Return to try
            }//end of number format catch


            //create for loop which checks each character throughout the string
            for (intCount = 0; intCount < strNumber.length(); intCount++)
                {
                    charLetter = strNumber.charAt(intCount); //charLetter is the alphanumeric value of a character in the string at the point dictated by intCount


                    if (!(charLetter >= '0') && (charLetter <= '9' ) //if charLetter is not between 0 and 9
                            || (charLetter == '.')) //or charLetter is not a full stop
                        {
                            bolNumeric = false; //Set bolNumeric as false
                        }//end of if construct
                }//end of for loop

            if (!bolNumeric) //if bolNumeric is false
                {
                    System.out.println("Incorrect input format! The balance must be numbers only!"); //Error message
                    bolSuccessful = false; //Set bolSuccessful as false
                }//end of if construct

    }while (!bolSuccessful); //While bolSuccessful is false, return to top


    return strNumber; //return strNumber to be used in main method
    //end of do method
}//end of getBalanceValidation method

NumberFormatException を使用したためかどうかはわかりません (double には他に何かありますか?)

どうもありがとう

4

3 に答える 3

0

正規表現を使用すると、はるかに簡単になります。

bolNumeric = strNumber.matches("[1-9][0-9]*(\\.[0-9]{1,2})?");

説明: 最初の数字は 1 から 9 の範囲内でなければなりません。次に、必要なだけ (なしを含む) 他の番号が続く場合があります。オプションで、ドットが続き、少なくとも 1 桁、最大 2 桁の数字が続きます。

于 2013-04-29T10:15:37.603 に答える
0

ブール式に 2 つのエラーがあります。

if (!(charLetter >= '0') && (charLetter <= '9' ) || (charLetter == '.')) 

この条件は次と同等です。

if ((charLetter < '0') && (charLetter <= '9' ) || (charLetter == '.')) 

これは次のように簡略化できます。

if ((charLetter < '0') || (charLetter == '.')) 

したがって、!式の最初の 2 つの部分に適用する必要があります

if (!( (charLetter >= '0') && (charLetter <= '9') ) || (charLetter == '.')) 

さらに、.は数値ではないため、この式は と同等です。

if (!( (charLetter >= '0') && (charLetter <= '9') )) 

あなたはおそらくそうでは&&なかった||でしょう:

if (!( (charLetter >= '0') && (charLetter <= '9' ) ) && (charLetter != '.'))

つまりif(not_a_number AND not_a_full-stop)

于 2013-04-29T10:08:09.690 に答える
0

double number = input.nextDouble();の代わりにできますstrNumber = input.next();。これにより、 の代わりにnumberas を直接入力できます。doubleString

InputMismatchExceptioncatch ブロックで処理する必要がありますが、問題ありません。が含まれていることを確認するために検証する必要はありません.

于 2013-04-29T10:15:28.570 に答える