【发布时间】:2013-10-16 16:12:29
【问题描述】:
有没有办法在 switch 语句中使用关系运算符(、>=)?
int score = 95;
switch(score) {
case (score >= 90):
// do stuff
}
上面的例子(显然)不起作用
【问题讨论】:
标签: java operators switch-statement relational
有没有办法在 switch 语句中使用关系运算符(、>=)?
int score = 95;
switch(score) {
case (score >= 90):
// do stuff
}
上面的例子(显然)不起作用
【问题讨论】:
标签: java operators switch-statement relational
它永远不会起作用。您首先应该了解switch 的作用。
它将执行符合switch参数的情况下的语句。
在这种情况下,score 是一个参数,它是 95,但 score>=90 将始终计算为 true 或 false,并且永远不会匹配整数。
您应该改用if 语句。
Java 也不允许 booleans 在 switch case 中使用,所以是的。
【讨论】:
只是否
int score = 95;
switch(score) {
case (score >= 90):
// do stuff
}
您将int 值传递给switch。所以案例的必须在int 值中,其中
(score >= 90)
转boolean。
你的案子是if else的好候选人
【讨论】:
不,你不能。
来自jls-14.11
The type of the Expression must be char, byte, short, int, Character, Byte, Short, Integer, String, or an enum type (§8.9), or a compile-time error occurs.
关系运算符 (,>=) 会导致 boolean,这是不允许的。
以下所有条件都必须为真,否则会发生编译时错误:
与 switch 语句关联的每个 case 常量表达式都必须可分配(第 5.2 节)到 switch 表达式的类型。
与 switch 语句关联的两个 case 常量表达式不能具有相同的值。
没有开关标签为空。
同一个switch语句最多可以关联一个默认标签。
【讨论】:
不幸的是,否,尽管您可以通过将多个不带 break 的 case 语句分组并在范围结束时实现代码来使用 casefall(有点 hacky):
int score = 95;
switch(score) {
..
case 79: System.out.println("value in 70-79 range"); break;
case 80:
..
case 85: System.out.println("value in 80-85 range"); break;
case 90:
case 91:
case 92:
case 93:
case 94:
case 95: System.out.println("value in 90-95 range"); break;
default: break;
}
恕我直言,在您的特定情况下使用 if 会更合适。
【讨论】:
docs for switch-case 语句状态:
switch 语句仅基于单个整数、枚举值或字符串对象测试表达式。
所以没有布尔值。这样做没有任何意义,因为您只有两个值:true 或 false。
你可以做的是编写一个方法来检查分数,然后返回 switch 可以处理的类型之一
例如:
enum CheckScore {
SCORE_HIGHER_EQUAL_90,
...
}
public CheckScore checkScore(int score) {
if(score >= 90) {
return SCORE_HIGHER_EQUAL_90;
} else if(...) {
return ...
}
}
然后在你的 switch 中使用它:
switch(checkScore(score)) {
case SCORE_HIGHER_EQUAL_90:
// do stuff
}
...或者你可以直接使用if, else-if, else!
【讨论】:
显然,这作为一种语言结构是不可能的。但是,只是为了好玩,我们可以自己实现它!
public class Switch<T, V> {
public static interface Action<V> {
V run();
}
private final T value;
private boolean runAction = false;
private boolean completed = false;
private Action<V> actionToRun;
public Switch(T value) {
this.value = value;
}
static public <T, V> Switch<T, V> on(T value) {
return new Switch<T, V>(value);
}
public Switch<T, V> ifTrue(boolean condition) {
runAction |= condition;
return this;
}
public Switch<T, V> ifEquals(T other) {
return ifTrue(value.equals(other));
}
public Switch<T, V> byDefault(Action<V> action) {
this.actionToRun = action;
return this;
}
public Switch<T, V> then(Action<V> action) {
if (runAction && !completed) {
actionToRun = action;
completed = true;
}
return this;
}
public V getResult() {
if (actionToRun == null) {
throw new IllegalStateException("none of conditions matched and no default action was provided");
}
return actionToRun.run();
}
}
Switch 接受任何值以打开,然后提供匹配布尔条件(ifTrue 方法)或完全匹配(ifEquals 方法)的功能。只需要为后一个功能提供一个开启值。
条件建立后,用户调用getResult获取结果。
例如,我们可以创建一个方法来告诉我们它对我们的分数的看法:
String tellMeMyScore(int score) {
return Switch.<Integer, String> on(score).byDefault(new Action<String>() {
public String run() {
return "really poor score";
}
}).ifTrue(score > 95).then(new Action<String>() {
public String run() {
return "you rock!";
}
}).ifTrue(score > 65).then(new Action<String>() {
public String run() {
return "not bad, not bad";
}
}).ifEquals(42).then(new Action<String>() {
public String run() {
return "that's the answer!";
}
}).getResult();
}
这个简单的测试:
for (int score : new int[] { 97, 85, 66, 55, 42, 32, 4 }) {
System.out.println(score + ": " + tellMeMyScore(score));
}
打印出来:
97: you rock!
85: not bad, not bad
66: not bad, not bad
55: really poor score
42: that's the answer!
32: really poor score
4: really poor score
【讨论】:
如果您需要自己使用 switch,这可能会对您有所帮助,
char g ='X';
int marks = 65;
switch(marks/10)
{
case 1:
case 2:
case 3:
case 4: g = 'F';
break;
case 5: g = 'E';
break;
case 6: g = 'D';
break;
case 7: g = 'C';
break;
case 8: g = 'B';
break;
case 9:
case 10: g = 'A';
break;
}
System.out.println(g);
它是这样工作的,
if(marks<50)
g='F';
else if(marks<60)
g='E';
else if(marks<70)
g='D';
else if(marks<80)
g='C';
else if(marks<90)
g='B';
else if(marks<=100)
g='A';
【讨论】: