Java Switch 不兼容类型 Boolean Int

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2898068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 23:20:07  来源:igfitidea点击:

Java Switch Incompatible Types Boolean Int

javabooleanswitch-statementcompiler-errors

提问by iTEgg

I have the following class:

我有以下课程:

public class NewGameContract {

public boolean HomeNewGame = false;

public boolean AwayNewGame = false;

public boolean GameContract(){

    if (HomeNewGame && AwayNewGame){
        return true;
    } else {
        return false;
    }
}
}

When I try to use it like so:

当我尝试像这样使用它时:

            if (networkConnection){

            connect4GameModel.newGameContract.HomeNewGame = true;

            boolean status = connect4GameModel.newGameContract.GameContract();

            switch (status){

                case true:
                    break;

                case false:
                    break;
            }
            return;
        }

I am getting the error:

我收到错误:

incompatible types found: boolean required: int on the following
`switch (status)` code.

What am I doing wrong?

我究竟做错了什么?

回答by chris

you don't want to switchon a boolean, just use a simple if/else

您不想switch使用布尔值,只需使用简单的if/else

if (status) {
  ....
} else {
  ....
}

edit : switchis only used for ints, chars, or enums (i think that's all, maybe there are others?) edit edit : it seems shortand byteare also valid types for switching, as well as the boxed versions of all of these (Integer, Short, etc etc)

编辑:switch仅用于intS,charS或enumS(我认为这是所有的,也许还有其他?)编辑编辑:好像shortbyte也适用类型的开关,以及所有这些的盒装版本(IntegerShort,等等等等)

回答by polygenelubricants

You can't switch on a boolean(which only have 2 values anyway):

您无法打开 a boolean(无论如何只有 2 个值):

The Java Language Specification clearly specifies what type of expression can be switch-ed on.

Java 语言规范明确指定了可以使用的表达式类型switch

JLS 14.11 The switch statement

JLS 14.11 switch 语句

SwitchStatement:
    switch ( Expression ) SwitchBlock

The type of the Expressionmust be char, byte, short, int, Character, Byte, Short, Integer, or an enumtype, or a compile-time error occurs.

SwitchStatement:
    switch ( Expression ) SwitchBlock

的类型Expression必须是char, byte, short, int, Character, Byte, Short, Integer, 或enum类型,否则会发生编译时错误。

It is a lot more readable and concise to simply use an ifstatement to distinguish the two cases of boolean.

简单地使用一个if语句来区分 的两种情况会更具可读性和简洁性boolean

回答by Shailesh Saxena

Switch statements in Java can use byte, short, char, and int (note: not long) primitive data types or their corresponding wrapper types. Starting with J2SE 5.0, it became possible to use enum types. Starting with Java SE 7, it became possible to use Strings.

Java 中的 switch 语句可以使用 byte、short、char 和 int(注意:不是 long)原始数据类型或其相应的包装类型。从 J2SE 5.0 开始,可以使用枚举类型。从 Java SE 7 开始,可以使用字符串。

回答by John Smith

Switch takes an integer value, and a boolean cannot be converted to an integer.

Switch 取整数值,布尔值不能转换为整数。

In java, a boolean is a type in its own right, and not implicitly convertible to any other type (except Boolean).

在 java 中,布尔值本身就是一种类型,不能隐式转换为任何其他类型(布尔值除外)。

回答by duffymo

Can't use boolean in switch, only int. Please read the Java docs for the switch statement.

不能在 switch 中使用 boolean,只能使用 int。请阅读有关 switch 语句的 Java 文档。

回答by Dónal Boyle

In Java, switch only works for byte, short, char, int and enum. For booleans you should use if/else as there are a very limited number of states.

在 Java 中,switch 仅适用于 byte、short、char、int 和 enum。对于布尔值,您应该使用 if/else,因为状态数量非常有限。