0

我很难确保在我的程序中正确验证双打。用户可以输入要存入帐户的金额,该金额应该是双倍的(我知道,这不是我应该使用的,但它是分配指南的一部分)。从理论上讲,用户应该能够存入任何金额——不仅仅是 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 (还有其他的东西吗?)

非常感谢

4

3 回答 3

0

您的布尔表达式中有2 个错误:

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

这个条件等价于:

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

可以简化为:

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

所以!应该应用于表达式的前两部分

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();. 这将允许您number直接输入 asdouble而不是String.

你必须InputMismatchException在你的 catch 块中处理,你很高兴。您无需验证即可检查是否包含..

于 2013-04-29T10:15:28.570 回答
0

使用正则表达式会容易得多:

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

解释:第一个数字必须在 1-9 之间。然后你想要多少(包括没有)其他数字可能会跟随。可选地后跟一个点,然后是至少一个,最多 2 个数字。

于 2013-04-29T10:15:37.603 回答