-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”))) –