3

I am a student learning Java and am stuck on the last part of a homework problem. I need to write a conditional statement that prints whether the roll of the dice was a Yahtzee or not (all five dice are equal to one another). I cannot figure out how to do this since I used ints and not boolean. I know I cannot cast an int into boolean and up to this point this casting is the only way I know how to change variables. Any help would be appreciated since the ways to achieve what I am looking for have been more than I can understand at this point. This is what I have and the issue is in the second to last line.

import java.util.Random;

public class FiveDice_JLR
{
   //-----------------------------------------------------------------
   //  Generates random numbers in various ranges.
   //-----------------------------------------------------------------
   public static void main(String[] args)
   {
      Random generator = new Random();
      int die1, die2, die3, die4, die5;

      die1 = generator.nextInt(6)+1;
      System.out.println("Die 1: " + die1);

      die2 = generator.nextInt(6)+1;
      System.out.println("Die 2: " + die2);

      die3 = generator.nextInt(6)+1;
      System.out.println("Die 3: " + die3);

      die4 = generator.nextInt(6)+1;
      System.out.println("Die 4: " + die4);

      die5 = generator.nextInt(6)+1;
      System.out.println("Die 5: " + die5);


      **if(die1==die2&==die3==die4&==die5&&);**
      {
      System.out.println("Yahtzee!!!!");
      }

   }
JenRut
  • 57
  • 4
  • 1
    `if(die1==die2&==die3==die4&==die5&&);` has syntax errors. Don't put a `;` at the end and remove the last `&&`. Apart from that, you should use `&&` instead of `&`. – Aloso Sep 25 '16 at 23:33
  • I've reopened this question since the posted code has a lot more errors than the duplicate alone covered; although it still falls under the typo category. Related: http://stackoverflow.com/questions/5564410/difference-between-and – Jason C Sep 26 '16 at 02:22

3 Answers3

5

If you use just &, that is the bitwise AND, therefore it compares the bits of all the numbers. What you want is &&, which is logical AND:

if(die1 == die2 && die2 == die3 && die3 == die4 && die4 == die5) {
    // Something here...
}

Also note that each && operator just like the == operator, requires 2 operands to properly compare


Assuming all your values are positive, another method to check if all numbers are equal to each other is to add them all up and check if the sum is equal to 5 times one of the values.

if((die1 + die2 + die3 + die4 + die5) == die1 * 5) {
    // Something here...
}

This is doing the same thing and is shorter, but is prone to error because you may in the future decide to generate negative numbers (for some reason), or decide to use more dice and forget to change 5 to the appropriate number, etc. In general, just stick to the first one

smac89
  • 39,374
  • 15
  • 132
  • 179
  • In this case the operands are booleans, so `&` is a Boolean AND. – user207421 Sep 25 '16 at 23:21
  • && is the short circuit version of AND; it's the correct thing to use. The answer as written is correct. – duffymo Sep 25 '16 at 23:22
  • @duffymo Both & and && would be correct. This answer does not identify a problem and misdescribes the operation of the OP's code. – user207421 Sep 25 '16 at 23:23
  • @EJP The operands are not booleans – smac89 Sep 25 '16 at 23:23
  • The operands are `die1==die2` etc. These are booleans, – user207421 Sep 25 '16 at 23:24
  • No, && is preferred b/c of the short circuit behavior. If you don't know what that is I'd suggest you investigate. – duffymo Sep 25 '16 at 23:24
  • @EJP I see what you mean, but I agree with duffy that short circuit behaviour of `&&` might be desirable in this case – smac89 Sep 25 '16 at 23:25
  • http://stackoverflow.com/questions/4014535/differences-in-boolean-operators-vs-and-vs. – duffymo Sep 25 '16 at 23:26
  • @smac89 Please explain how using & instead of && results in more code or more prone-ness to error. And when are you going to fix the error about bitwise operators? – user207421 Sep 25 '16 at 23:46
  • @EJP nice one. I was caught up in operator precedence – smac89 Sep 26 '16 at 00:03
  • 4
    You guys really arguing over this? You're confusing a newbie. It's `&&` not `&`, just leave it at that for someone new and such a simple example. – ShadowGod Sep 26 '16 at 00:11
  • 1
    Thank you very much for all of your help. Special thanks to ShadowGod because all the comments were more than a little confusing for me at this level. Hopefully one day I will be able to follow along with the rest of you all! Thanks again. – JenRut Sep 26 '16 at 01:12
2

There are two operators that will be important for checking that the values of all your dice are the same: the conditional AND operator (&&) and the equality operator (==). The Java documentation includes some good details of what these are, and how to use them.

There are a couple of things to keep in mind with these. Both of these operators are binary operators. They operate on two operands. e.g. a == b compares the value of a to b. Similarly, a && b evaluates to true if both a and b are true.

Of course, this presents a challenge when we want to compare the values of more than two operands. Say we have three variables, a, b, and c. How would we check that they all have the same value? (For simplicity, we'll assume they're all primitive types).

We know how to check that a and b are the same: a == b.

We also know how to check that b and c are the same: b == c.

If both of these are true then, by the transitive property of equality, we know that a must also be equal to c. How do we check two conditions within a single statement? Using the conditional AND operator, of course:

a == b && b == c

This was a simple example with just three variables, but this can be extended to any number of variables (say, to the values of 5 dice). Give it a try, and see if you can get it working using these two operators.

nbrooks
  • 18,126
  • 5
  • 54
  • 66
  • 1
    Thank you for this explanation, it really clarified this concept for me. You could teach this if you wanted. Your explanation was better than any professor I know of. – JenRut Sep 26 '16 at 01:16
  • @JenRut So this is the answer you should have accepted. Don't accept answers with poor terminology. The correct name of the `&` operator here is [Boolean logical operator](http://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.22.2). – user207421 Sep 26 '16 at 01:18
1

The statement you are looking for is

if(die1==die2 && die2==die3 && die3==die4 && die4==die5)

Or, for @smac89's and &duffymo's benefit, a version with &:

if(die1==die2 & die2==die3 & die3==die4 & die4==die5)

which will run slower because of fully evaluating everything, but is otherwise semantically identical.

user207421
  • 305,947
  • 44
  • 307
  • 483