简体   繁体   English

为什么我的计数器不能正确计数? 虽然我的 if 语句没有填充,但它增加了 +1

[英]why won't my counter count properly ? it adds +1 although my if statement isnt filled

So my counter is supposed to not count +1 for the string ":DD" that's why I wrote that only if the length of my string is 2 the counter should add +1.所以我的计数器不应该为字符串":DD"计数+1,这就是为什么我写的只有当我的字符串长度为2计数器才应该加+1。 But it adds +1 although the string length is 3. Why is that?但是它增加了+1,尽管字符串长度是3。这是为什么呢?

PS: I've put length() <=3 in the first if statement, for another else if that comes after the 2nd if statement. PS:我已经将length() <=3放在第一个if语句中, else if在第二个if语句之后出现另一个else if

int counter = 0;
String tgt = ":DD";

for (int i = 0; i < a.size(); i++) {
    if (tgt.length() <= 3 && tgt.charAt(0) == ':' || tgt.charAt(0) == ';') {
        if (tgt.length() == 2 &&  
            tgt.charAt(1) == ')' || tgt.charAt(1) == 'D') {
                counter += 1;
        }
    }
}

The && operator has higher precedence than || &&运算符的优先级高于|| , messing up your intended logic. ,搞乱你的预期逻辑。

The Java operators page lists Java operators in order of precedence. Java 运算符页面按优先顺序列出了 Java 运算符。

Because of this, it's as if parentheses are around the two && operands:因此,就好像括号围绕着两个&&操作数:

if ((tgt.length() <= 3 && tgt.charAt(0) == ':') || tgt.charAt(0) == ';')
{
    if ((tgt.length() == 2 &&  tgt.charAt(1) == ')') || tgt.charAt(1) == 'D')

In the second if condition, the && may return false, but the second (index 1) character is 'D' , so the whole condition is true and your counter gets updated.在第二个if条件中, &&可能返回 false,但第二个(索引 1)字符是'D' ,因此整个条件为true并且您的计数器得到更新。

Place explicit parentheses around your ||在您的||周围放置明确的括号operands, which is what I think you intended.操作数,这就是我认为你的意图。

if (tgt.length() <= 3 && (tgt.charAt(0) == ':' || tgt.charAt(0) == ';'))
{
    if (tgt.length() == 2 && (tgt.charAt(1) == ')' || tgt.charAt(1) == 'D'))

This is a case operator precedence (source: oracle.com ) not matching with the expectations we as developers might have.这是一个案例运算符优先级(来源: oracle.com与我们作为开发人员的期望不符。 The && -operator has a higher precedence than || &&运算符的优先级高于|| . . This means that这意味着

tgt.length() <= 3 && tgt.charAt(0) == ':' || tgt.charAt(0) == ';'

evaluates to the same value as评估为相同的值

(tgt.length() <= 3 && tgt.charAt(0) == ':') || tgt.charAt(0) == ';'

(Notice the parentheses around (... && ...) ) (注意(... && ...)周围的括号)

If we want that the ||如果我们想要|| -operator is evaluated first, we have to add explicit parentheses: -operator 首先被评估,我们必须添加显式括号:

tgt.length() <= 3 && (tgt.charAt(0) == ':' || tgt.charAt(0) == ';')

(Notice the parentheses around (... || ...) ) (注意(... || ...)周围的括号)

(likewise with the 2nd if -condition) (同样与第二个if

Ideone demo Ideone 演示

The problem seems to be that your second if isn't correctly set.问题似乎是您的第二个 if 设置不正确。

If I understand correctly you want to check if the second letter is a ")" or a "D" unless the length is of two.如果我理解正确,您想检查第二个字母是“)”还是“D”,除非长度为两个。

what your checking is if it's length = 2 and 2nd letter ")" OR that the second letter is simply equal to a "D"您要检查的内容是长度 = 2 和第二个字母“)”还是第二个字母等于“D”

you would fix it by changing the if to if ((&& tgt.charAt(1) == ')'|| tgt.charAt(1) == 'D') && tgt.length() != 2) {您可以通过将 if 更改为if ((&& tgt.charAt(1) == ')'|| tgt.charAt(1) == 'D') && tgt.length() != 2) {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM