2017-03-28 57 views
-5

我運行此程序時遇到了問題,如果我使用int它可以工作,但對於字符串,它不會。Java:在字符串問題時執行

import java.util.Scanner; 

public class userinput_swutch_do_while { 

    public static void main(String[] args) {  

     String password= null; 
     Scanner value = new Scanner(System.in); 
     do { 
      System.out.println("Enter the password for ATM: "); 
      password = value.nextLine(); 
      System.out.println("You put : "+password); 

     } while (password !="55"); 


     System.out.println("You got it!"); 

    } 

} 
+4

的[?我如何在Java中比較字符串(可能的複製http://stackoverflow.com/questions/513832/how-do-i-compare -strings-in-java) –

回答

0

在JAVA,字符串相等正確測試使用equals()equalsIgnoreCase();
話雖這麼說,你可以試試這個:

import java.util.Scanner; 

public class userinput_swutch_do_while { 

public static void main(String[] args) {  

    String password= null; 
    Scanner value = new Scanner(System.in); 
    do { 
     System.out.println("Enter the password for ATM: "); 
     password = value.nextLine(); 
     System.out.println("You put : "+password); 

    } while(!(password.equals("55")));// string equality 


    System.out.println("You got it!"); 

    } 

} 
+0

感謝您的回覆,我嘗試過使用equals()或equalsIgnoreCase();但我的問題是,如果無論如何,我們可以使用「==」或「!=」這個特定的程序? –

+0

如果它回答你的問題,則將其標記爲已回答;順便說一句歡迎 –

+0

「==」和「!=」比較對象參考(粗略),而等於實際內容以便快速回答 –