新手請問一下:
我按了 q 卻無法跳出while 回圈,是怎麼了?
謝謝 public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
BufferedReader temp;
String str;
boolean b=false ;
while(b==false){
        System.out.print("按 q 結 束");
temp = new BufferedReader(new InputStreamReader(System.in));         
        str = temp.readLine();          
        System.out.println(str);
        if (str=="q") {b=true;}                  

}

解决方案 »

  1.   

     if (str.equals("q")) {b=true;}     
      

  2.   

    if ("q".equals(str)){
        b=true;
    } 这样写可以避免 空指针异常...  换一下比较好.
      

  3.   

    再請教一個問題喔:
    若我希望輸入"a" 之後按enter
    再等待讓人家輸入"hello" 
    請問要怎麼改呢?    public static void main(String[] args) throws IOException {
            // TODO Auto-generated method stub        
            BufferedReader temp;
            String str;        
            boolean b=false ;
            while(b==false){
                System.out.print("1.按 q 結 束");    
                System.out.print("2.按 a 之後按 Enter 請再輸入hello");    
            temp = new BufferedReader(new InputStreamReader(System.in));            
                str = temp.readLine();             
                System.out.println(str);
                if (str=="q") {b=true;}                        
            } 
        }
      

  4.   

    写一个while循环啊  直到输入‘q’的话  就break这个循环就可以了