2015-12-01 24 views
-1

我希望系統接受字符M或S大寫和小寫,但它不斷循環。我哪裏做錯了?多個條件,而錯誤

MS1 = JOptionPane.showInputDialog("Please enter Marital Status M or S. "); 
    while(MS1.substring(0) != "S" && MS1.substring(0) != "s" && MS1.substring(0) != "M" && MS1.substring(0) != "m"){ 
     JOptionPane.showMessageDialog(frame, "Please enter S or M.only"); 
     MS1 = JOptionPane.showInputDialog("Please enter Marital Status M or S. "); 
    } 

回答

0

,我們只能比較字符串與全光照空==或!=,該函數檢查字符串的實際內容,==操作符檢查是否引用的對象是相等的。請注意,字符串常量通常是「interned」的,這樣兩個具有相同值的常量實際上可以與==進行比較,但最好不要依賴它。

while(MS1.substring(0).equalsIgnoreCase("S") && MS1.substring(0).equalsIgnoreCase("M") { 
+0

請包括一些文字說明您爲什麼認爲這可能會解決OP的問題。幫助別人理解。 – APC