나는 그것이 모두이 있는지 확인하려면 사용자의 입력을받을 :다중 입력 유효성 통합 자바
- 최소 값보다
- 더는
내가 다음 코드를 쓴 수 이것을 달성하기 위해서는 그것이 있어야하는 것보다 더 복잡하게 보입니다. 질문을 통합 할 수있는 방법은 입력 번호이며 그 숫자는 또는 이와 유사한 두 부분 유효성 검증보다 적은 숫자입니까?
// function prompts user for a double greater than number passed in
// continues to prompt user until they input a number greater than
// the minimum number
public static double getInput(double minimumInput) {
Scanner scan = new Scanner(System.in);
double userInput;
System.out.print("Enter a number greater than " + minimumInput + ": ");
while (!scan.hasNextDouble()){
String garbage = scan.next();
System.out.println("\nInvalid input.\n");
System.out.print("Enter a number greater than " + minimumInput + ": ");
} // end while
userInput = scan.nextDouble();
while (userInput <= minimumInput) {
System.out.println("\nInvalid input.\n");
userInput = getInput(minimumInput);
}
return userInput;
} // end getInput
너무 많이 있기 때문에 제가 답변의 마지막 부분을 제거하는 것이 좋습니다 지식의 수준에있는 사람을위한 정보. – talex
그랬습니까; 그의 질문의 "스타일"측면에 더 집중했다. 유효한 제안, 감사합니다! – GhostCat
의견에 감사드립니다. –