Java - AND operator not working

前端 未结 4 1669
清歌不尽
清歌不尽 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:37

    Your logic is incorrect. The loop will continue as long as both values don't match - as soon as one value matches, the loop exits. We can invert your logic to show this:

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

    which is logically equivalent to what you have.

    What you want is this:

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

    which says "Continue while any variable does not have the desired value"

提交回复
热议问题