2016-08-04 10 views
-1

ユーザ入力の文字列をキューに格納して全体を印刷するプログラムを作ったが、文字列 'end'が印刷されないようにする問題がある文字列キューを印刷するとき最後の文字列をJavaで印刷しないようにする

public class Test { 

    protected static String testinfo; 
    Test() { 
     testinfo= "blank"; 
    } 

    public static void setTest(String newInfo){ 
     testInfo = newInfo; 
    } 

    public static String getTest(){ 
     return testInfo; 
    } 

    private static Scanner input = new Scanner(System.in); 

    public static void main(String[] args) { 
     Queue<String> queue = new LinkedList<String>(); 
     String newInfo; 

     System.out.println("Inset information for the test or quit by typing end "); 

     while (true) { 
      System.out.println("Insert information: "); 
      newInfo = input.nextLine(); 

      Test.setTest(newInfo); 

      queue.offer(Test.getTest()); 

      if (newInfo.equals("end")){ 
       break; 
      } 
     } 

     while(queue.peek() !=null) { 
      String x = queue.poll(); 
      if(x.contains("end")) { 
       queue.remove("end"); 
      } 
      System.out.println(x + " "); 
     } 
    } 
} 
+0

なぜキューに追加するのですか? –

+0

qから削除しますが、常に出力してください。 'println'を' else'に入れてください。 –

+0

while(!(queue.peek.equals(( "" end "))) –

答えて

0

は、whileループでは、すでにユーザが入力した文字列を提供してきました - endをキューに、あなたがブレーク条件をチェックする前に。実際には、String - endがエンキューされます。

ループはあなたが

while (true) { 
     System.out.println("Insert information: "); 
     newInfo = input.nextLine(); 
     if (newInfo.equals("end")){ 
      break; 
     } 
     Test.setTest(newInfo); 
     queue.offer(Test.getTest()); 
    } 

注直面している問題解決する必要がありながら、中に再配置文:私はあなたのコードの他の部分をリファクタリングdin'tを。

関連する問題