The operator < is undefined for the argument type(s) boolean, int

佐手、 提交于 2019-12-14 04:12:29

问题


I am new to processing and I am having trouble with this. I keep getting an error message for the bolded part of the code below. Is my syntax wrong?

void block(int x, int y, int s, color tinto) {
    fill(tinto);
    for (int i = 0; i < 3; i++) {
        triple(x, y+i*s, s, tinto);
    }
    if (0 < i < 3 && 6 < i < 9) {  // HERE
        tinto = 255;
    }
    else {
        tinto = tinto - 200;
    }
}

回答1:


In Java, to check if a variable is in a range you have to divide the statement into two parts, like this:

if (0 < i && i < 3 && 6 < i && i < 9){

}

This specific code will never be true, however, because you're asking for it to be in two different ranges. Perhaps you meant to check for either range?

if (0 < i && i < 3 || 6 < i && i < 9){

}

Note the || or operator instead of the && and operator.




回答2:


The syntax is not valid, and I think you're expression is wrong anyway. You say i has to be within a range AND within another. I think you mean to write that it could be between one OR the other.

Example of valid syntax: instead of 0 < i < 3, write i > 0 && i < 3.

Try this:

if ( (i > 0 && i < 3) || (i > 6 && i < 9) )

Note that the following (which is what you were trying to do apparently) will never be evaluated to true because it cannot be within both ranges.

if ( (i > 0 && i < 3) && (i > 6 && i < 9) ) // incorrect



回答3:


This isn't valid java expression. Try:

if (0<i && i<3 && 6<i && i<9){



回答4:


There are two different problems with this code snippet. First, you have defined the 'i' variable as in "int" inside of the for loop. This instance of 'i' is no longer defined once you exit that for loop -- so the if statement below does not refer to that instance. To overcome this, define 'i' before the for loop...

int i;
for ( i=0; i<3; I++ ) {
    ...
}
if ( i ...

which brings me to the second error. The syntax "0 < i < 3" is not correct. In c/c++ the operators are executed one at a time ... so in this case the first "<" operator will be evaluated as "0 < i" and the result will be a Boolean (which will always be 'true' in your particular code snippet). But the important point is that the result is a Boolean. Next the code will attempt to evaluate that result with the next part of the statement -- "true < 3", which just doesn't make any sense, and so that receives a compiler error. In your code snippet, there is no exit to the for loop until the value of i reaches 3, so this second "if" statement is unneeded. But if you did want to test to see whether i was between 1 and 2 (inclusive), then you would have to break those up into individual tests...

if ( 0 < i && i < 3 ...

Lastly ... if the value of i is between 1 and 2 (inclusive), then it cannot also be between 7 and 8 (inclusive) .. therefore the if statement as you coded it will always be false even after we correct the syntax.



来源:https://stackoverflow.com/questions/27347209/the-operator-is-undefined-for-the-argument-types-boolean-int

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!