Java 带有布尔值的 switch 语句不起作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19351236/
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
Switch statement with boolean is not working?
提问by
Why is my compiler telling me:
为什么我的编译器告诉我:
Incompatible Types:
不兼容的类型:
Required: Boolean
必需:布尔值
Found: Int
发现:Int
under case 0 & case 1
在案例 0 和案例 1 下
For instance:
例如:
public void test(boolean isOn){
switch (isOn){
case 0:
if (isOn){
System.out.println("its on");
}
break;
case 1:
if (!isOn){
System.out.println("its off");
}
break;
default:
System.out.println("I don't know!");
}
}
}
Driver Class:
驱动类:
Club me = new Club();
me.test(true);
回答by SLaks
As the error clearly states, numbers are not booleans.
正如错误明确指出的那样,数字不是布尔值。
You want true
and false
.
你想要true
和false
。
回答by Rohit Jain
You are switching on boolean
type, and your cases are using int
types. But even though you change your cases to have boolean
types, that wouldn't work. You cannot switch on boolean
type. And that wouldn't make any sense as using an if-else
would be easier anyways:
您正在打开boolean
类型,而您的案例正在使用int
类型。但是即使您将案例更改为具有boolean
类型,那也行不通。您无法打开boolean
类型。这没有任何意义,因为if-else
无论如何使用 an会更容易:
if (isOn) {
System.out.println("its on");
} else {
System.out.println("its off");
}
Note that there is no "I don't know!"
case here. A boolean
type can have either true
or false
value. This is another reason, why switch-case
is not for boolean
type. There is no default case.
请注意,这里没有"I don't know!"
案例。一个boolean
类型可以有true
或false
值。这是另一个原因,为什么switch-case
不是boolean
类型。没有默认情况。
You can also condense it to a single statement by using a conditional expression:
您还可以使用条件表达式将其压缩为单个语句:
public void test(boolean isOn) {
System.out.println(isOn ? "its on" : "its off");
}
回答by Sage
switch (isOn)
: switching boolean
and want to case with int
e.g., case 0
!
switch (isOn)
: 切换boolean
并想与int
例如,case 0
!
According to the JLS section 14.11: for a switch ( Expression ) SwitchBlock
:
根据JLS 第 14.11 节:对于 switch ( Expression ) SwitchBlock
:
Expression can only be
char, byte, short, int, Character, Byte, Short, Integer, String, or an enum type
other wise a compile-time erroroccurs.According to the specification followings are also must be true:
- Every case constant expression associated with a switch statement must be assignable to the type of the switch Expression.
- No two of the
case
constant expressions associated with a switch statement may have the same value.- No
switch
label isnull
.- At most one default label may be associated with the same switch statement
表达式只能以
char, byte, short, int, Character, Byte, Short, Integer, String, or an enum type
其他方式发生编译时错误。根据规范,以下内容也必须为真:
- 与 switch 语句关联的每个 case 常量表达式都必须可分配给 switch 表达式的类型。
case
与 switch 语句关联的任何两个常量表达式都不能具有相同的值。- 没有
switch
标签是null
。- 最多一个默认标签可以与同一个 switch 语句相关联
Hence, switch expression can't be float, double or boolean
. TO answer the question why?: boolean true false
are meaningful using with if-else
, e.g., if(true) then do
. Floating point numbers (float
, double
) are not a good candiadtes for switch as exact comparison is often broken by rounding errors. e.g. 0.11 - 0.1 == 0.01
is false.
因此,switch 表达式不能是 float、double 或boolean
. 回答为什么?:布尔值true false
是有意义的,使用 with if-else
,例如,if(true) then do
. 浮点数 ( float
, double
) 不是 switch 的好选择,因为精确比较经常被舍入错误破坏。例如0.11 - 0.1 == 0.01
是假的。
回答by Gigatron
This is not C++ where 1 and 0 get implicitly converted to/from true and false. Switching on boolean is also a waste of time even if you could do it; just write a simple if/else statement, or use the ? :
construct.
这不是 C++,其中 1 和 0 隐式转换为/从真和假。即使你能做到,打开布尔值也是浪费时间;只需编写一个简单的 if/else 语句,或使用? :
构造。
回答by Tubig
Just convert the boolean to number of 1 and 0.
只需将布尔值转换为 1 和 0 的数字。
public void test(boolean isOn){
int trueOrFalse;
if(isOn == true){
trueOrFalse = 1;
}else{
trueOrFalse = 0;
}
switch (trueOrFalse){
case 1:
if (isOn){
System.out.println("its on");
}
break;
case 0:
if (!isOn){
System.out.println("its off");
}
break;
default:
System.out.println("I don't know!");
}
}
回答by Gagan Mani
In Java, Switch does NOT work with Boolean.
在 Java 中,Switch 不适用于 Boolean。
Accepted variable types are : char,byte,short,int,String,Character,Byte,Short,Integer,enum
接受的变量类型有:char、byte、short、int、String、Character、Byte、Short、Integer、enum
回答by ZhekaKozlov
ˉ\_(ツ)_/ˉ
ˉ\_(ツ)_/ˉ
switch (Boolean.hashCode(isOn)) {
case 1231: System.out.println("its on"); break;
case 1237: System.out.println("its off"); break;
}
回答by Abhishek Ghosh
In Java switch works only with integer literals and those which could be possibly promoted to integer literals such as char.
在 Java 中,开关仅适用于整数文字和那些可能被提升为整数文字的文字,例如 char。
Moreover in Java boolean type has only two values either true or false. This is unlike the situation in C/C++ where true is any value not equals to zero and false is zero.
此外,在 Java 中布尔类型只有真或假两个值。这与 C/C++ 中的情况不同,其中 true 是任何不等于零的值,而 false 是零。
Moreover your code is a bit redundant
此外,您的代码有点多余