나는 한 줄에 정수의 목록을 입력하는 사용자가 필요합니다, 그래서이 쓴 : 영원히 반복 계속하지만Scanner를 사용하여 한 줄에서 정수 목록을 읽을 때 무한 루프가 발생하는 이유는 무엇입니까?
public static Integer[] readIntegers()
{
Scanner input = new Scanner(System.in);
List<Integer> list = new ArrayList<Integer>();
do list.add(input.nextInt());
while(input.hasNextInt());
return list.toArray(new Integer[list.size()]);
}
을! 스캐너에 숫자가없는 경우 hasNextInt()
은 false
을 반환 할 예정입니까? 이전 방법을 수정하려면 어떻게해야합니까? 또는 대신 nextLine()
을 사용해야하고 String을 유출해야합니까? 프로그램이 다음 입력을 기다리고 있도록
http://stackoverflow.com/questions/1794281/java-infinite-loop-using-scanner-in-hasnextint –
당신은 EOF를 표준 입력으로 가져 왔습니까? [ctrl + z in windows, ctrl + d in linux] – amit
전체 프로그램을 'Ctrl + Z'로 끝내지 않겠습니까? –