I’m trying to exclude the zero in this nested for loop here using != 0; but it is not doing anything. I’m trying to get the probability of each out come of 2 six sided dice when rolled. I must figure out the amount of times they are rolled first, but a die doesn’t have a zero in it, so I must exclude it. I can’t figure out why this doesn’t work.
for( die2 = 0; die2 <= 6 && die2 != 0; die2++)
for( die1 = 0; die1 <= 6 && die1 != 0; die1++)
System.out.println("Die 2: " + (die2 * userInputValue) + " " + "Die 1: " + (die1 * userInputValue));
You should start your loops at 1, not at 0.
Let’s look at your current code:
Why aren’t you hitting the do something line? This chunk of code is equivalent(*) to
Notice that the first condition check if
die2is 0, butdie2will always be 0 upon entering the loop because that’s what you set. Instead, you should usewhich will skip 0 because you never set
die2ordie1to 0 in the first place.(*) This rewriting of a while loop isn’t exactly equivalent to a for loop, because this rewriting doesn’t preserve the semantics of the
continuestatement, but your code doesn’t use continue, so in this case it’s equivalent to the original.