我不明白为什么下面的代码:
public Image getLetter(String letterToGet)
{
System.out.println("é" == "e");
System.out.println("Received: " + letterToGet);
if("\u00e9" == letterToGet.toLowerCase()); {
letterToGet = "SPECIALACCTAIGUESPECIAL";
}
if("\u00e8" == letterToGet.toLowerCase()) {
letterToGet = "SPECIALACCTGRAVESPECIAL";
}
System.out.println("searching for " + letterToGet + " in the hashmap");
return languageMap.get(letterToGet.toLowerCase());
}
可以返回以下输出
Traduction following ArrayList: [e, é, è]
Received: e
searching for SPECIALACCTAIGUESPECIAL in the hashmap
Received: é
searching for SPECIALACCTAIGUESPECIAL in the hashmap
Received: è
searching for SPECIALACCTAIGUESPECIAL in the hashmap
按照此逻辑,为什么此行返回false?
System.out.println("\u00e9" == "e");
最佳答案
输出意外的原因是第一个if
之后多余的分号。
目前,您有
if("\u00e9" == letterToGet.toLowerCase()); {
letterToGet = "SPECIALACCTAIGUESPECIAL";
}
其中
letterToGet
的分配超出了if
的范围,因此无论letterToGet
的值如何,它都将运行。