import java.io.*;
public class TerminateWhen
{
public static void main(String args[]) throws IOException
{
BufferedReader 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");
}
}问题是即使输入"x",循环也不会退出。
发布于 2010-09-15 15:33:29
试试!str.equals("x")!
发布于 2010-09-15 15:37:06
在处理字符串时,要注意“标准”比较运算符。
str != "x"比较两个引用,而不是字符串的内容。使用equals方法比较字符串内容。
发布于 2010-09-15 15:40:07
您必须检查是否有equals()
https://stackoverflow.com/questions/3715537
复制相似问题