은 항상 nextLine()을 사용하여 입력을 읽고 문자열을 구문 분석하는 것을 선호합니다.
next()
을 사용하면 공백 앞에 오는 내용 만 반환됩니다. nextLine()
은 현재 줄을 반환 한 후 스캐너를 자동으로 아래로 이동합니다.
nextLine()
의 데이터를 구문 분석하는 유용한 도구는 str.split("\\s+")
입니다.
public class Scientist {
private String name;
private String field;
private String greatIdeas;
public static void main(String[] args) {
String scientists[] = new String[100];
int scientistCount = 0;
Scanner input = new Scanner(System.in);
do{
String answer;
System.out.println("Enter the name of the Scientist: ");
scientists[scientistCount]=input.nextLine();
System.out.println(scientistCount);
System.out.println("Would You like to add another Scientist?");
scientistCount++;
}
while(input.nextLine().equalsIgnoreCase("Y"));
input.close();
}
}
정확히 어떤 종류의 문제가 있습니까? – Berger
do-while 루프를 사용하여 배열을 채울 수 없으며 초기 루프가 끝나면 더 이상 과학자 이름을 입력하라는 메시지가 표시되지 않습니다. @Berger –