0

I have two same while loops, the logic is simple. if the value is 7 or the parameter it should stop. it works with the following method

while(true) {
    int value = diceRoll();
    if(value ==7 || value == point){
        return value;

    }
    System.out.print(value + " ");
}

But with the below method, it doesn't do what it needs too. But it's pretty much the same as the above method.

public static int secondStage(int point) {
    int x = 0;
    while((diceRoll() != 7) || (diceRoll() != point)) {
        System.out.print(diceRoll() + " ");
        x= diceRoll();
    }
    return x;
}
Matt Strom
  • 698
  • 1
  • 4
  • 23
Fruit
  • 31
  • 6

2 Answers2

1

Two primary issues with your second while condition.

  1. You should be using an and operator instead of an or for the boolean expression to evaluate correctly.
  2. Too many lines of code containing diceRoll(). You can achieve the purpose by just one call in a given iteration.

Replace your while condition

while((diceRoll() != 7) || (diceRoll() != point))

with

while(x != 7 && x != point)

Overall,

while(x != 7 && x != point) {
   x = diceRoll();
   System.out.print(x + " ");
}

should work.

VHS
  • 9,534
  • 3
  • 19
  • 43
  • I want it to terminate if diceRoll is seven or the point, so shouldn't i use Or here? why is using x works but just using the method calls right away doesn't? – Fruit May 12 '20 at 19:31
  • `I want it to terminate if diceRoll is seven or the point` - what that also means is you want to continue rolling the dice until x is not 7 AND x is also not equal to point. – VHS May 12 '20 at 19:34
  • no it means terminate if X is 7 or X is the point. say x is 2. then if x != 7 terminate. if x != point terminate – Fruit May 12 '20 at 19:36
  • I am confused. Do you want to keep rolling the dice if x = 7 or x = point? – VHS May 12 '20 at 19:41
  • sorry for not being clear, so the dice should stop if x = 7. the dice should stop if x = point – Fruit May 12 '20 at 19:47
  • 1
    my apologies I made this confusing with my earlier comment on my answer. VHS is right, in this case of using `||`, only the first condition would be evaluated (if it's true). To evaluate both conditions the `&&` would be required. – Matt Strom May 12 '20 at 19:49
  • oh i see. this makes ssense. thank you – Fruit May 12 '20 at 19:54
0

I second @DavidZimmerman's comment.

Having multiple diceRoll calls in your while condition may produce some hefty logic bugs. Each of those calls is potentially getting a different result, so your where clause will not be consistant at all.

To add on to @VHS's answer:

  1. Initialize your variable like you did in your last code block
int x = 0;
  1. Take diceRoll out of your where clause as such and call diceRoll() like you have been:
where (x != 7 && x != point) {
    // do work
    x = diceRoll();
}
Matt Strom
  • 698
  • 1
  • 4
  • 23
  • yes thank you. @VHS thinks using && makes sense but to me || makes sense. what do you think? – Fruit May 12 '20 at 19:40
  • Oh actually, sorry just reading the comments. @Fruit I think @VHS is right. In this case the first condition will be evaluated first, and since x will not be 7, it will continue. To evaluate both conditions the `&&` would be necessary, otherwise it's essentially an infinite loop – Matt Strom May 12 '20 at 19:46
  • this is very confusing, but the first method using while(True) then if(x == 7 || x == point) here || is the correct one right? – Fruit May 12 '20 at 19:49
  • Yea, so the `!=` is what makes this confusing. (It confused me too for a second there). When you negate the conditions you have to negate the binary operator as well to retain the same outcome. While what you say is true, and that may work, I would recommend having the condition inside the where clause as this is more clean and a better coding practice. – Matt Strom May 12 '20 at 19:56