有没有更好的办法,以避免重复OR的语句if条件?我有if超过8个字符串的OR语句。
if(type.equalsIgnoreCase(anotherString1) ||
type.equalsIgnoreCase(anotherString2) ||
type.equalsIgnoreCase(anotherString3)){
return true;
}
Run Code Online (Sandbox Code Playgroud)
寻找更好的方法来编写if多条OR语句或如何避免
简而言之,您可以使用不同的值构建Set以测试小写(toLowerCase()如果需要,可以使用)。
然后,将您的声明更改为:
if (<set_name>.contains(type.toLowerCase()))
Run Code Online (Sandbox Code Playgroud)
The interest is also that you add some semantic to your code. Your set will have a name, something functionally suited for your case (allowedUserTypes, elementTypesToProcess or whatever) and can be set somewhere useful for reuse, if relevant.
| 归档时间: |
|
| 查看次数: |
97 次 |
| 最近记录: |