错误:类型不兼容:布尔值无法转换为int
问题描述:
我遇到错误:不兼容的类型:在Android Studio中编译android应用时,布尔值无法转换为int 错误.问题仅在于一种方法.
I'm getting the error: incompatible types: boolean cannot be converted to int error while compiling the android app in Android Studio. The problem is in only one method.
private static int get_wx_inx(String str) {
boolean equalsIgnoreCase = str.equalsIgnoreCase("木");
int equalsIgnoreCase2 = str.equalsIgnoreCase("火");
if (str.equalsIgnoreCase("土")) {
equalsIgnoreCase2 = true;
}
if (str.equalsIgnoreCase("金")) {
equalsIgnoreCase2 = 3;
}
return str.equalsIgnoreCase("水") ? 4 : equalsIgnoreCase2;
}
我不知道怎么了.您能帮我解决问题吗?预先感谢.
I don't know what could be wrong. Can you help me to figure out the problem. Thanks in advance.
答
您的问题尚不明确,但您可能希望这样做:
Your question is not bit clear but you might want this:
private static int get_wx_inx(String str) {
if(str.equalsIgnoreCase("木")) {
return 0;
} else if(str.equalsIgnoreCase("火")) {
return 1;
}else if(str.equalsIgnoreCase("土")) {
return 2;
} else if(str.equalsIgnoreCase("金")) {
return 3;
} else if(str.equalsIgnoreCase("水")) {
return 4;
} else {
return 0;// write default return here. If every condition goes false then this default will be return.
}
}