是否有更简洁的方法来执行此 if 语句?

Is there a cleaner way of doing this if statement?

if(input.charAt(i) == '0' || input.charAt(i) == '1' || input.charAt(i) == '2') {
}

有没有办法压缩这个 if 条件,或者没有?

您可以检查字符是否与公共 String 中的任何索引匹配。喜欢,

if ("012".indexOf(input.charAt(i)) > -1) {

}

可能更具可读性(在 java 9+ 中)

if (Set.of('0', '1', '2').contains(input.charAt(i))) {

}

您可以通过将字符查找结果分配给一个变量(但仍然需要三个相等性检查)来使其更短

char c = input.charAt(i);
if(c == '0' || c == '1' || c == '2') {
}

您可以查看其他答案,例如创建 Set/Array 并执行 contains 检查相等性检查的数量是否会在未来增加。 IMO,三张支票按原样写应该没问题。