WA / Runtime Error 이유!

kylechang912 Reply 5 years 20 weeks ago
Enter an id: 11 Please enter a correct id Enter an id: 10 Please enter a correct id Enter an id: 1 input의 이 코드 이후에 Enter an id: 11 Please enter a correct id Enter an id: 10 Please enter a correct id Enter an id: 1 Wrong choice, try again: Wrong choice 저 문구가 출력되어서 WA가 뜨는것 같은데, 어떻게 하면 고칠 수 있을까요?
Hyunwoo Reply 5 years 19 weeks ago
구현하신 코드중에 아래의 부분에서 public static int incorrectId(int id) { while (id < 1 || id >= 10) { System.out.print("Please enter a correct id"); System.out.println(""); System.out.print("Enter an id: "); id = in.nextInt(); } return id; } id = in.nextInt(); 이 부분에서 에러가 나타납니다. 나타나는 에러는 아래의 에러입니다. Exception in thread "main" java.util.NoSuchElementException at java.util.Scanner.throwFor(Scanner.java:862) at java.util.Scanner.next(Scanner.java:1485) at java.util.Scanner.nextInt(Scanner.java:2117) at java.util.Scanner.nextInt(Scanner.java:2076) at Main.incorrectId(Main.java:45) at Main.main(Main.java:33) 생성하신 스캐너 객체를 멤버변수로 만들지 마시고 main 함수 내에서 선언 후 스캐너 객체가 쓰이는 함수마다 함수의 매개변수로 넘겨주는 것도 하나의 방법이 될 것입니다.