开发者

Java Buffered Reader

 import java.io.*;

 public class TerminateWhen
 {
   public static void main(String args[]) throws IOException
   {
     Buffe开发者_JAVA技巧redReader br = new BufferedReader(new InputStreamReader(System.in));

     String str = "";
     System.out.println("Type \"x\" to exit..");
     do {
       str = br.readLine();
       System.out.println(str);
       }
     while(str!="x");
   }
 }

the problem is even if will type the "x", the loop will not exit..


Try !str.equals("x") !!!


Beware "standard" comparison operators when you're working with strings.

str != "x"

compares the two references, not the string's contents. Use the "equals" method to compare the string contents.


You must check for equals()

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜