-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 + " ");
}
}
}
なぜキューに追加するのですか? –
qから削除しますが、常に出力してください。 'println'を' else'に入れてください。 –
while(!(queue.peek.equals(( "" end "))) –