Java 我能';我不能用If语句解决这个问题
这段代码根本不起作用。我知道的一切我都试过了Java 我能';我不能用If语句解决这个问题,java,string-comparison,Java,String Comparison,这段代码根本不起作用。我知道的一切我都试过了 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 Be
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);
}
}
}
}
其输出为:
Cheat Selection: k1lla1otch3at
NOT Cheat 1
请帮忙 像这样使用
if(CheatSelection.equals(CheatCodes[0]))
要在java中比较字符串对象,请使用.equals()方法而不是“==”运算符
如果您想忽略该案例,请使用下面的代码
if(CheatSelection.equalsIgnoreCase(CheatCodes[0]))
您知道如何正确比较字符串的值吗?这就是if语句的问题所在