2017-11-26 13 views
-2

이것은 문자 인 경우 입력 및 루프의 유효성을 검사하는 데 사용되는 코드의 일부이지만 문자가 입력되면 무한 루프가 발생하지만 정수이면 익숙한. 나는 무한 루프를 일으키는 것이 무엇인지 모르지만 어떤 도움을 주시면 감사하겠습니다.자바 무한 루프, 유효성 검사 동안

System.out.println("Please type in a mark and enter -1 to end the program"); 

    while (mark != -1) { 

     if (in.hasNextInt()) { 
      mark = in.nextInt(); 
     } 
     else { 
      System.out.println("Please input an integer: "); 

     } 
+0

'System.out.println ("정수를 입력하십시오 :"); in.nextLine();' –

답변

1

경우 입력 할 문자 :

if (in.hasNextInt()) 

false를 반환, 당신은 다른 사람,이 루프 때 in.hasNextInt()는 여전히 다른 사람을 입력하고 반복 거짓로 이동합니다 영원히.