【问题标题】:JOptionPane not reading the string [duplicate]JOptionPane 未读取字符串 [重复]
【发布时间】:2023-03-29 08:00:01
【问题描述】:

我正在尝试使用通过 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.");
        }
}   

【问题讨论】:

  • 您的问题很快就会结束...但是您不能将字符串与== 进行比较,您必须使用.equals() 方法。所以像这样...numsystem1.equalsIgnoreCase("Binary") || ....
  • 非常感谢,我的朋友!

标签: java string swing


【解决方案1】:

您比较字符串的方式是错误的。在java中你必须像这样使用.equals()方法

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

【讨论】:

    【解决方案2】:

    显然,我使用的是== 而不是equals() 方法。这就是问题所在。

    【讨论】:

      猜你喜欢
      • 2010-11-30
      • 2016-06-03
      • 2018-02-05
      • 1970-01-01
      • 2015-06-08
      • 2016-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多