if Statement not evaluating boolean correctly in Java?

后端 未结 2 1730
误落风尘
误落风尘 2021-01-28 10:06

I\'m making a simple game where you move a ball on a screen and try to avoid other blocks on the screen.

The problem i\'m having is that when I try to evaluate whether y

相关标签:
2条回答
  • 2021-01-28 10:14

    You need

    if (sect == true) {          // compares sect with true and checks the result
    

    instead of

    if (sect = true) {           // sets sect to true and then checks it
    

    Even better would be to use

    if (sect) {                 // checks sect
    
    0 讨论(0)
  • 2021-01-28 10:27

    You are mistakenly assigning the true boolean literal to your variable in if block expression:

    if(sect = true)
    

    due to which, the expression is always evaluated to true. This is the reason why you should never compare boolean variables with true or false. Just using sect here would be enough.

    Change it to:

    if (sect)
    
    0 讨论(0)
提交回复
热议问题