-3
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;


public class Example {

    public static void main(String[] args) {

        String name;

        name = JOptionPane.showInputDialog(null, "ENTER TEST BELOW:"," ",3 );

        if (name == "TEST") {

            JOptionPane.showMessageDialog(null, "Welcome " + name + ", It works "," ",  1, new ImageIcon("Pictures/Example.jpg"));

        }

        else {

            JOptionPane.showMessageDialog(null,"Welcome " + name + ", It doesn't work."," ",  1, new ImageIcon("Pictures/Example.jpg"));

        }
    }

}
4

1 回答 1

2

而不是将字符串与此进行比较:

name == "TEST"

你应该使用这个:

name.equals("TEST")

一个著名的问题解释了您需要这样做的原因。本质上,==比较name​​和是否"TEST"是同一个对象。它们不是同一个对象,但它们具有相同的内容,并且equals测试相同的内容。

于 2013-08-29T00:07:33.730 回答