2016-05-03 38 views
-1

我想使用通過JOptionPane獲得的字符串的值。但是,讀取字符串時出現問題。我在這裏做錯了什麼?JOptionPane不能讀取字符串

import javax.swing.JOptionPane; 
public class convertNumber123 { 
     public static void main(String[] args){ 
      String numsystem1; 
      numsystem1 = JOptionPane.showInputDialog("Please enter the numeral system that you want to convert from: binary, octal, decimal or hexadecimal."); 
      if (numsystem1 == "Binary" || numsystem1 == "Octal" || numsystem1 == "Decimal" || numsystem1 == "Hexadecimal") 
       System.out.println (numsystem1 + "it is!"); 
      else 
       System.out.println ("Please, enter the correct system name."); 
     } 
} 
+3

您的問題將很快關閉......但您無法將字符串與'=='進行比較,您必須使用'.equals()'方法。所以像這樣......'numsystem1.equalsIgnoreCase(「Binary」)|| ....' – 3kings

+0

非常感謝,我的朋友! – daniilcul8r

回答

2

您比較字符串的方式是錯誤的。在Java中,你必須使用類似這樣的

if (numsystem1.equals("Binary") || numsystem1.equals("Octal") || numsystem1.equals("Decimal") || numsystem1.equals("Hexadecimal")) 
+1

非常感謝! – daniilcul8r

-1

.equals()方法很顯然,我是用==代替equals()方法。這是問題所在。