代码不长,麻烦哪位好心人看下指正下错误哈。   我就想实现键盘输入pro1的时候打印pro1 输入pro2的时候打印pro2 输入exit的时候退出程序 并且可以循环输入直直退出 实际运行效果是不管输入什么打印的都是"Reinput"import java.io.*;public class TestWhile{
public static void main(String[] args){
try{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();
while(s != "exit"){
while(s == "pro1"){
System.out.println("pro1");
s = br.readLine();
}
while(s == "pro2"){
System.out.println("pro2");
s = br.readLine();
}
while((s != "pro1")&&(s != "pro2")&&(s != "exit")){
System.out.println("Reinput");
s = br.readLine();
}
}
}
catch(IOException e){
e.printStackTrace();
}
System.out.println("The end");
}
}

解决方案 »

  1.   

    字符串使用 equals 判等
      

  2.   

     == 判断是不是同一个引用用一个字符串,当然是false了。
      

  3.   

    如果你想实现你想要的功能,建议用java.util.Scanner,现在这个用InputStreamReader的方法不给力
      

  4.   

    huntor说的很对,这是给你改的import java.io.*;public class TestWhile{
        public static void main(String[] args){
            try{
                BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                String s = br.readLine();
                while(!s.equals("exit")){
                    while(s.equals("pro1")){
                        System.out.println("pro1");
                        s = br.readLine();
                    }
                    while(s.equals("pro2")){
                        System.out.println("pro2");
                        s = br.readLine();
                    }
                    while(!(s.equals("pro1"))&&(!s.equals("pro2"))&&(!s.equals("exit"))){
                        System.out.println("Reinput");
                        s = br.readLine();
                    }
                }
            }catch(IOException e){
                e.printStackTrace();
            }
            System.out.println("The end");
        }
    }