프로그램에서 복식의 유효성을 올바르게 검사하는 데 어려움이 있습니다. 사용자는 계좌에 입금 할 금액을 입력 할 수 있습니다. 두 배가되어야합니다 (사용해야하는 것이 아니지만 양도 지침의 일부 임). 이론적으로 사용자는 £ 30뿐만 아니라 £ 15.23과 같은 금액을 입금 할 수 있어야합니다. 이것은 숫자를 허용하지만 많은 문제를 일으키는 정류장의 입력을 방지하는 현재의 유효성 확인입니다. 여기 (단순해야 함) double double validation
내가 지금까지 가지고있는 코드 :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을 사용한 적이 있기 때문에인지 확실하지 않다 (두 배에 뭔가가있다?)
많은 감사
'여러 가지 문제'를 정의하십시오. 어떤 것이 * 때문에 ... 있는지 확실하지 않습니다 ...? 진짜 질문이 아닙니다. – EJP
변수가 'true'로 설정되었다는 코멘트를 정말 싫어합니다. : // set bolSuccessful and bolNumeric to true. – Henrik
나는 의견을 너무 싫어하지만 내가 말했던 것처럼 그것은 할당을위한 것입니다. 나는 방금 미친 듯이 말하라는 말을 들었다. –