2016-11-25 7 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;} 
    } 
関連する問題