2016-11-25 2 views
0

현재 데이터 입력을 설정하라는 프로젝트를 진행 중입니다. 데이터 입력 모드에서 사용자는 과학자 데이터를 루프에 입력하라는 메시지가 표시됩니다. 사용자가 'y'로 응답하면 다른 과학자를 입력하라는 메시지가 표시됩니다. 이 작업을 수행하는 가장 좋은 방법은 사용자가 이상을 결정할 때까지 배열을 채우는 do-while 루프를 사용하는 것입니다. 나는 배열에 이름을 작성Do-While 루프로 채우기 배열

  1. 에 문제가 있어요, 그리고
  2. 프로그램은 초기 루프 후 이름을 표시하지 않습니다. 여기

내가 무엇을 가지고 : while(input.nextLine().equalsIgnoreCase("Y"));

+0

정확히 어떤 종류의 문제가 있습니까? – Berger

+0

do-while 루프를 사용하여 배열을 채울 수 없으며 초기 루프가 끝나면 더 이상 과학자 이름을 입력하라는 메시지가 표시되지 않습니다. @Berger –

답변

1

은 항상 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(); 
     } 
    } 
0

변경 while(input.next().equalsIgnoreCase("Y"));를이 내가 찾을 수있는, 그것을 할 수있는 당신이

String scientists[] = new String[100]; 
    int scientistCount = 0; 
    Scanner input = new Scanner(System.in);  
    boolean again = true; 

    while(again){ 
     System.out.println("Enter the name of the Scientist: "); 
     scientists[scientistCount]=input.nextLine(); 
     scientistCount++; 
     System.out.println(scientistCount); 
     System.out.println("Would You like to add another Scientist? y/n"); 
     if(!input.nextLine().equalsIgnoreCase("y")){ 
      again = false; 
     } 
    } 
    input.close(); 
0

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.next().equalsIgnoreCase("Y")); 
     input.close(); 
    } 
} 
0

또 다른 방법을 의미 해결책인가 arraylist를 사용하는 것이 더 간단하고, 부울을 변경 한 후 중단되는 일반적인 while 루프가 있습니다. 다음 예제를 참조하십시오 :

ArrayList<String> scientists = new ArrayList<String>(); 
    Scanner input = new Scanner(System.in); 
    boolean keepGoing = true; 

    while(keepGoing){ 
     System.out.println("Enter the name of the Scientist: "); 
     scientists.add(input.nextLine()); 
     System.out.println("Would You like to add another Scientist? (y/n)"); 

     if(input.nextLine().toLowerCase().equals("y")){continue;} 
     else{keepGoing = false;} 
    }