首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我不能用If语句解决这个问题

我不能用If语句解决这个问题
EN

Stack Overflow用户
提问于 2013-11-25 02:03:34
回答 1查看 35关注 0票数 0

这段代码根本不起作用。我知道的我都试过了!

代码语言:javascript
运行
复制
public static String[] CheatCodes = {"k1lla1otch3at", "H1gh$c0re!!", "wr1t3H1gh$C0r3$"};
if (actionconsole == 5){// Cheats
        String CheatSelection = JOptionPane.showInputDialog(null, "What Is You Code?"+eol+"Type It In Below"+eol+"WARNING! If You Get It Wrong Your Leader Will Lose Health!", "Cheats", JOptionPane.WARNING_MESSAGE);
        System.out.println("  Cheat Selection: "+CheatSelection);

        if(CheatSelection == CheatCodes[0]){
            warriershealth = 2;
            archershealth = 1;
            leaderhealth = 2;
            System.out.println("  successful!");
            JOptionPane.showMessageDialog(null, "Lots Of Enemies Have Been Killed!", "Cheat Successful", JOptionPane.INFORMATION_MESSAGE);
        }else{
            System.out.println("  NOT Cheat 1");
            if(CheatSelection == CheatCodes[1]){
                ScorePlus = 200;
                System.out.println("  successful!");
                JOptionPane.showMessageDialog(null, "Your Score Has Been Increased!", "Cheat Successful", JOptionPane.INFORMATION_MESSAGE);
            }else{
                if(CheatSelection == CheatCodes[2]){
                    WriteHighScore();
                    System.out.println("  successful!");
                    JOptionPane.showMessageDialog(null, "The High Scores Have Been Writen!", "Cheat Successful", JOptionPane.INFORMATION_MESSAGE);
                }
            }
        }
    }

此命令的输出为:

代码语言:javascript
运行
复制
Cheat Selection: k1lla1otch3at

NOT Cheat 1

请帮帮我!

EN

回答 1

Stack Overflow用户

发布于 2013-11-25 02:08:24

像这样使用

代码语言:javascript
运行
复制
    if(CheatSelection.equals(CheatCodes[0]))

要比较java中的字符串对象,请使用.equals()方法而不是"==“运算符

如果你想忽略大小写,使用下面的代码

代码语言:javascript
运行
复制
    if(CheatSelection.equalsIgnoreCase(CheatCodes[0]))
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20177924

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档