0

下面的代码只是不断给出 else 错误消息,我不知道出了什么问题..

Boolean loop2 = true;
userInput = scan.next();

while (loop2) {
    userInput = scan.next();
    if(userInput.toLowerCase() == "") {
        out.println(
            "Please pick a option! it's in the 'Commands' <= type this in!"
        );
        loop2 = true;
    } else if(userInput.toLowerCase() == "fight") { 
        Fights(); loop2 =   false;
    } else if(userInput.toLowerCase() == "train") { 
        Training(); loop2 = false; 
    } else if(userInput.toLowerCase() == "bosses") { 
        Bossing(); loop2 =  false; 
    } else if(userInput.toLowerCase() == "shops") { 
        ShopsAll(); loop2 = false; 
    } else if(userInput.toLowerCase() == "commands") { 
        Menu(); loop2 = false; 
    } else {
        out.println("ERROR MESSAGE!!.");
    }
}
4

2 回答 2

0

利用:

"fight".equals(userInput.toLowerCase());

==比较内存引用

于 2013-11-29T08:34:40.437 回答
0

在 java 中,您必须将字符串与.equals()or.equalsIgnoreCase()方法进行比较。永远不要使用==运算符来比较字符串!

于 2013-11-29T08:36:01.033 回答