不明白为什么这不起作用输入字符串

时间:2018-02-25 00:03:55

标签: java string input joptionpane

对不起家伙我只是有一个简单的问题。我想弄清楚为什么当用户在第一个提示符下输入动物然后在第二个提示符下输入yes时,程序不会打印出“你的项目是驼鹿”。任何帮助都会非常感谢

    public static void main(String[] args) {
    // TODO Auto-generated method stub


    String Answer1;
    String Answer2;

    // Read in how much cash the user has
    Answer1 = JOptionPane.showInputDialog("Question 1: Is it an animal, vegetable, or mineral");
    Answer2 = JOptionPane.showInputDialog("Question 2: Is it bigger than a breadbox");


    if (Answer1 == "animal" && Answer2 == "yes") {
        JOptionPane.showMessageDialog(null, "Your item is: A Moose");
    }
}

}

1 个答案:

答案 0 :(得分:0)

在Java中,你不会将字符串与双等号符号进行比较,它只是用于整数。您可以使用方法equalsequalsIgnoreCase来比较字符串,如果有大写字母则无关紧要。

if (Answer1 == "animal" && Answer2 == "yes") {
        JOptionPane.showMessageDialog(null, "Your item is: A Moose");
    }

因此,在您的情况下,您可以使用equals

if (Answer1.equals("animal") && Answer2.equals("yes")) {
        JOptionPane.showMessageDialog(null, "Your item is: A Moose");
    }