gpt4 book ai didi

Java switch - 不同的条件运算符

转载 作者:行者123 更新时间:2023-11-29 03:12:52 26 4
gpt4 key购买 nike

我发现了一些与此“有点”相关的问题,但我不确定我的需求在这些问题中是否完全符合要求。因此,我要做的是用 SWITCH/CASE 清理以下 IF/ELSE IF 语句。有没有一种方法可以在 Java 开关中使用多个条件运算符,或者它是严格的 boolean 值?

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

最佳答案

正如 Kon 所说,您不能将其简化为一个极其简单的开关/外壳,但您可以这样做:

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

比较干净

关于Java switch - 不同的条件运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28356920/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com