gpt4 book ai didi

Java if else 语句改变字符串

转载 作者:行者123 更新时间:2023-12-01 14:58:45 25 4
gpt4 key购买 nike

Possible Duplicate:
How do I compare strings in Java?

我有以下代码,但由于某种原因它不会更改字符串

accountClass 返回字符串“L”

String accountClass = accClass.substring(4, 5);
if (accountClass == "A") {
accountClass = "Staff - A";
} else if (accountClass == "B") {
accountClass = "Student - B";
} else if (accountClass == "H") {
accountClass = "Personal - H";
} else if (accountClass == "K") {
accountClass = "60 Plus - K";
} else if (accountClass == "L") {
accountClass = "Business - L";
} else if (accountClass == "P") {
accountClass = "Charity - P";
} else if (accountClass == "Q") {
accountClass = "AIB Subsidiary - Q";
} else if (accountClass == "X") {
accountClass = "Irish State Spons & Sub's - X";
} else if (accountClass == "Y") {
accountClass = "Other Companies & Orgs - Y";
} else if (accountClass == "Z") {
accountClass = "Impersonal & Gen. Ledger - Z";
}

我只是想知道是 == 导致了问题还是其他原因?

最佳答案

始终使用 equals() 方法检查字符串是否相等。对于对象 == 检查两个引用变量是否引用同一个实例。使用 String.equals() 方法检查两个字符串是否有意义。

if (accountClass == "A") {

应该是

if (accountClass.equals("A")) {

其他 if 语句也是如此。

关于Java if else 语句改变字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13989252/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com