Java - AND operator not working

前端 未结 4 1670
清歌不尽
清歌不尽 2021-01-27 12:27

newbie here,
I have two variables which generate random numbers through .Random. I want them keep rolling until both variables generate two different values, si

4条回答
  •  终归单人心
    2021-01-27 12:44

    You're getting the logical result you describe, but it wasn't what you expect. Specifically, when either of your conditions evaluates to false the logical and will not evaluate to true. I think you wanted

    while (!(diceRolled1 == 5 && diceRolled2 == 4)) {
    

    which is while not dice1 equal to 5 and dice2 equal to 4. And then, using De Morgan's Laws that might also be expressed as

    while (diceRolled1 != 5 || diceRolled2 != 4) {
    

    which means loop while dice1 is not equal to 5 or dice2 is not equal to 4.

提交回复
热议问题