0
사용자 입력이 숫자인지 어떻게 확인합니까? 번호는 main 메소드에서 가져온 것이고 다른 메소드의 번호인지 검사됩니다. 입력이 숫자이면 true를 반환하고 그렇지 않으면 false를 반환해야합니다.다른 방법으로 사용자 입력 유효성 확인
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Please, enter a number: ");
int number = sc.nextInt();
validateInput(number);
}
public static int validateInput(int num) {
while(num <= 0)
if(!sc.hasNextInt())
System.out.print("INVALID INPUT, Try again...");
}
출력이되어야한다 : 나는 지금까지 무엇을 가지고
는
Please, enter a number: a
Invalid input, Try again...
Please, enter a number: 16
Valid input, thank you.
[문자열이 Java에서 정수를 나타내는 지 확인하는 가장 좋은 방법은 무엇입니까?] (http://stackoverflow.com/questions/237159/whats-the-best-way-to-check- to-a-string-as-an-in-java) – APerson
입력이 int가 아닐 때마다'sc.nextInt()'를 호출하면 예외가 발생합니다. –