首页 > 解决方案 > 错误:不兼容的类型:布尔值无法转换为 int

问题描述

我收到错误:不兼容的类型:在 Android Studio 中编译 android 应用程序时,布尔值无法转换为 int错误。问题只有一种方法。

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;
}

我不知道有什么问题。你能帮我找出问题所在吗?提前致谢。

标签: javaandroid

解决方案


您可以在 switch 语句中使用字符串并返回相应的 int 值。

https://docs.oracle.com/javase/7/docs/technotes/guides/language/strings-switch.html

因为我们不能有混合类型的返回值。您可以考虑 0(FALSE) 和 1(TRUE)。或任何这样的随机值。


推荐阅读