java将两条if语句汇总为一条
我有许多if
和if else
语句,如下代码所示。有没有可能对这个问题进行总结
if (zob.equals(""))
及
else if (!(nameShortestDistance.equals("ABC"))
&& !(nameShortestDistance.equals("DEF")))
一个if语句条件中的语句
代码:
if (zob.equals("")) {
int stops_number = number_stop_in_table(macD, con);
if (stops_number > 2) {
Analyzer analyzer = new Analyzer(host, user, password);
analyzer.analyze_data();
System.out.println("Result: " + stops_number);
}
maintain_records(speedD, macD, con,
shortestDistance, nameShortestDistance,
sto_nam);
} else if (!(nameShortestDistance
.equals("ABC"))
&& !(nameShortestDistance
.equals("DEF"))) {
int stops_number = number_stop_in_table(macD, con);
if (stops_number > 2) {
Analyzer analyzer = new Analyzer(host, user, password);
analyzer.analyze_data();
System.out.println("Result: "+ stops_number);
}
maintain_records(speedD, macD, con, shortestDistance,
nameShortestDistance, sto_nam);
}
# 1 楼答案
因此,您的方法变得更短,并且(希望)更具可读性。此外,如果这些方法是公开的,那么您可以单独对它们进行单元测试,这使生活更简单
# 2 楼答案
这可能会有帮助。不同类型操作员的列表。使用这些,你的选择是无穷无尽的。。。花点时间学习这些
从https://docs.oracle.com/javase/tutorial/java/nutsandbolts/opsummary.html复制
# 3 楼答案
对。它被称为“conditional OR operator”(也称为“逻辑或运算符”),
||
有一个关于它们的教程