Scanner s = new Scanner(System.in);
String userInput;
while(s.hasNext()) {
userInput = s.next();
out.println(userInput);
System.out.println(br1.readLine());
if(userInput == "End") {
break;
}
}请问这while为什么终止了还是运行不了下一行程序呢?即使输入End,跳出循环还是一样。程序一直在while卡着不动了。
还有就是,运行了Scanner以后,程序是否会一直等待输入,然后有一个输入后就自动关闭Scanner呢?

解决方案 »

  1.   

    while(s.hasNext()) 你这个有问题Scanner s = new Scanner(System.in);
    String userInput;
    while(s!="End") {   //while改成类似这样好了。
    ...
    }
      

  2.   

    while(s.hasNext())这里有什么问题呢?有点搞不懂,当没有输入的时候,s.hasNext()是返回false吗?如果是那就跳出循环了。觉得这样好奇怪。
      

  3.   

    userInput == "End"改为userInput .equals("End")
      

  4.   

    对的,楼上正解,调用String的equals方法来做判断