if (device1.getOnline() == 1){ sum1on++; }else { sum1unon++; } if ("lumi.remote.b186acn01".equals(device1.getDeviceType().getDeviceType())||"lumi.remote.b186acn02".equals(device1.getDeviceType().getDeviceType())||"lumi.remote.b286acn01".equals(device1.getDeviceType().getDeviceType())||"lumi.remote.b286acn02".equals(device1.getDeviceType().getDeviceType())||"lumi.sensor_switch.aq3".equals(device1.getDeviceType().getDeviceType())){ wuxian.add(device1); if (device1.getOnline() == 1){ wuxianon++; }else { wuxianunon++; } }else if ("lumi.ctrl_86plug.aq1".equals(device1.getDeviceType().getDeviceType())){ chazuo.add(device1); if (device1.getOnline() == 1){ chazuoon++; }else { chazuounon++; } }else if ("lumi.ctrl_dualchn".equals(device1.getDeviceType().getDeviceType())){ mokuai.add(device1); if (device1.getOnline() == 1){ mokuaion++; }else { mokuaiunon++; } }else if("lumi.ctrl_neutral1.v1".equals(device1.getDeviceType().getDeviceType())||"lumi.ctrl_neutral2.v1".equals(device1.getDeviceType().getDeviceType())){ danhuo.add(device1); if (device1.getOnline() == 1){ danhuoon++; }else { danhuounon++; } }else if("N0003".equals(device1.getDeviceType().getDeviceType())||"N0005".equals(device1.getDeviceType().getDeviceType())||"N0014".equals(device1.getDeviceType().getDeviceType())){ menshuo.add(device1); if (device1.getOnline() == 1){ menshuoon++; }else { menshuounon++; } }else if("N0004".equals(device1.getDeviceType().getDeviceType())||"N0009".equals(device1.getDeviceType().getDeviceType())){ anfang.add(device1); if (device1.getOnline() == 1){ anfangon++; }else { anfangunon++; } }else { } 优化为 switch 语句
时间: 2024-04-13 16:31:03 浏览: 87
你可以将上述代码优化为switch语句,如下所示:
```java
switch(device1.getDeviceType().getDeviceType()) {
case "lumi.remote.b186acn01":
case "lumi.remote.b186acn02":
case "lumi.remote.b286acn01":
case "lumi.remote.b286acn02":
case "lumi.sensor_switch.aq3":
wuxian.add(device1);
if (device1.getOnline() == 1) {
wuxianon++;
} else {
wuxianunon++;
}
break;
case "lumi.ctrl_86plug.aq1":
chazuo.add(device1);
if (device1.getOnline() == 1) {
chazuoon++;
} else {
chazuounon++;
}
break;
case "lumi.ctrl_dualchn":
mokuai.add(device1);
if (device1.getOnline() == 1) {
mokuaion++;
} else {
mokuaiunon++;
}
break;
case "lumi.ctrl_neutral1.v1":
case "lumi.ctrl_neutral2.v1":
danhuo.add(device1);
if (device1.getOnline() == 1) {
danhuoon++;
} else {
danhuounon++;
}
break;
case "N0003":
case "N0005":
case "N0014":
menshuo.add(device1);
if (device1.getOnline() == 1) {
menshuoon++;
} else {
menshuounon++;
}
break;
case "N0004":
case "N0009":
anfang.add(device1);
if (device1.getOnline() == 1) {
anfangon++;
} else {
anfangunon++;
}
break;
default:
break;
}
```
使用switch语句可以提高代码的可读性和可维护性。
阅读全文