为什么这两个代码片段有不同的输出?
它们之间的唯一区别是每个 if/else-if 语句周围的大括号,但这在这里无关紧要,对吧?
while (1){
if (i>=n&&j<0)
break;
else if (j<0)
if (Arr[i])
c++;
else if (i>=n)
if(Arr[j])
c++;
else if (Arr[i]==1&&Arr[j]==1)
c+=2;
i++;
j--;
}
..
while (1){
if (i>=n&&j<0){
break;
}
else if (j<0){
if (Arr[i])
c++;
}
else if (i>=n){
if(Arr[j])
c++;
}
else if (Arr[i]==1&&Arr[j]==1){
c+=2;
}
i++;
j--;
}
最佳答案
如果您正确格式化第一个代码片段
while (1){
if (i>=n&&j<0)
break;
else if (j<0)
if (Arr[i])
c++;
else if (i>=n)
if(Arr[j])
c++;
else if (Arr[i]==1&&Arr[j]==1)
c+=2;
i++;
j--;
}
然后可以看出else或者else if对应的是最接近的if语句。
关于c++ - else if 和大括号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44938819/