Java switch - 不同的条件运算符
Java switch - varying conditional operators
我发现了几个 "somewhat" 相关的问题,但我不确定我的需求是否完全符合这些问题。因此,我要做的是用 SWITCH/CASE 清理以下 IF/ELSE IF 语句。有没有办法在 Java 开关中使用多个条件运算符,或者它是严格的布尔值?
int alSize = Integer.parseInt(size);
if (alSize == 7 && (alType == 1 || alType == 3)){
disp = 0.12;
}else if (alSize == 9 && (alType == 1 || alType == 3 || alType == 4 || alType == 5 )){
disp = 0.27;
}else if (alSize == 9){
disp = 0.21;
}else if (alSize == 11 && (alType == 1 || alType == 3 || alType == 4 || alType == 5 )){
disp = 0.47;
}else if (alSize == 11){
disp = 0.41;
}else if (alSize == 12 && (alType == 1 || alType == 3 || alType == 4 || alType == 5 )){
disp = 0.75;
}else if (alSize == 12){
disp = 0.64;
}else if (alSize == 14 &&( alType == 1 || alType == 3 || alType == 4 || alType == 5 )){
disp = 1.10;
}else if (alSize == 14){
disp = 0.96;
}else if (alSize == 16 && (alType == 1 || alType == 3 || alType == 4 || alType == 5 )){
disp = 1.60;
}else if (alSize == 16){
disp = 1.40;
}else if (alSize == 18 && (alType == 1 || alType == 3 || alType == 4 || alType == 5 )){
disp = 2.60;
}else if (alSize == 18){
disp = 2.30;
}else if (alSize == 22 && (alType == 1 || alType == 3 || alType == 4 || alType == 5 )){
disp = 4.0;
}else if (alSize == 22){
disp = 3.60;
}else if (alSize == 30 && (alType == 1 || alType == 3 || alType == 4 || alType == 5 )){
disp = 9.9;
}else if (alSize == 30){
disp = 9.2;
}
case
子句中只能有一个表达式。所以不,你不能按照你想象的那样去做。
查看关于 switch
的 Javadocs
你能做的最接近的事情是:
switch (value)
{
case 1:
case 2:
//Operation if value is 1 or 2
break;
}
正如 Kon 所说,你不能将它简化成一个难以置信的简单 switch/case,但你可以这样做:
boolean typeIs1345 = (alType == 1 || alType == 3 || alType == 4 || alType == 5);
switch (alSize)
{
case 7:
if (alType == 1 || alType == 3)
disp = 0.12;
break;
case 9:
disp = typeIs1345 ? 0.27 : 0.21;
break;
case 11:
disp = typeIs1345 ? 0.47 : 0.41;
break;
case 12:
disp = typeIs1345 ? 0.75 : 0.64;
break;
case 14:
disp = typeIs1345 ? 1.1 : 0.96;
break;
case 16:
disp = typeIs1345 ? 1.6 : 1.4;
break;
case 18:
disp = typeIs1345 ? 2.6 : 2.3;
break;
case 22:
disp = typeIs1345 ? 4.0 : 3.6;
break;
case 30:
disp = typeIs1345 ? 9.9 : 9.2;
break;
}
}
比较干净
我发现了几个 "somewhat" 相关的问题,但我不确定我的需求是否完全符合这些问题。因此,我要做的是用 SWITCH/CASE 清理以下 IF/ELSE IF 语句。有没有办法在 Java 开关中使用多个条件运算符,或者它是严格的布尔值?
int alSize = Integer.parseInt(size);
if (alSize == 7 && (alType == 1 || alType == 3)){
disp = 0.12;
}else if (alSize == 9 && (alType == 1 || alType == 3 || alType == 4 || alType == 5 )){
disp = 0.27;
}else if (alSize == 9){
disp = 0.21;
}else if (alSize == 11 && (alType == 1 || alType == 3 || alType == 4 || alType == 5 )){
disp = 0.47;
}else if (alSize == 11){
disp = 0.41;
}else if (alSize == 12 && (alType == 1 || alType == 3 || alType == 4 || alType == 5 )){
disp = 0.75;
}else if (alSize == 12){
disp = 0.64;
}else if (alSize == 14 &&( alType == 1 || alType == 3 || alType == 4 || alType == 5 )){
disp = 1.10;
}else if (alSize == 14){
disp = 0.96;
}else if (alSize == 16 && (alType == 1 || alType == 3 || alType == 4 || alType == 5 )){
disp = 1.60;
}else if (alSize == 16){
disp = 1.40;
}else if (alSize == 18 && (alType == 1 || alType == 3 || alType == 4 || alType == 5 )){
disp = 2.60;
}else if (alSize == 18){
disp = 2.30;
}else if (alSize == 22 && (alType == 1 || alType == 3 || alType == 4 || alType == 5 )){
disp = 4.0;
}else if (alSize == 22){
disp = 3.60;
}else if (alSize == 30 && (alType == 1 || alType == 3 || alType == 4 || alType == 5 )){
disp = 9.9;
}else if (alSize == 30){
disp = 9.2;
}
case
子句中只能有一个表达式。所以不,你不能按照你想象的那样去做。
查看关于 switch
的 Javadocs你能做的最接近的事情是:
switch (value)
{
case 1:
case 2:
//Operation if value is 1 or 2
break;
}
正如 Kon 所说,你不能将它简化成一个难以置信的简单 switch/case,但你可以这样做:
boolean typeIs1345 = (alType == 1 || alType == 3 || alType == 4 || alType == 5);
switch (alSize)
{
case 7:
if (alType == 1 || alType == 3)
disp = 0.12;
break;
case 9:
disp = typeIs1345 ? 0.27 : 0.21;
break;
case 11:
disp = typeIs1345 ? 0.47 : 0.41;
break;
case 12:
disp = typeIs1345 ? 0.75 : 0.64;
break;
case 14:
disp = typeIs1345 ? 1.1 : 0.96;
break;
case 16:
disp = typeIs1345 ? 1.6 : 1.4;
break;
case 18:
disp = typeIs1345 ? 2.6 : 2.3;
break;
case 22:
disp = typeIs1345 ? 4.0 : 3.6;
break;
case 30:
disp = typeIs1345 ? 9.9 : 9.2;
break;
}
}
比较干净