Page 1 of 1

## 3 Replies - 913 Views - Last Post: 06 June 2012 - 06:16 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=281861&amp;s=7bc9f6a1b431c1883b5a2ae3c4392ec5&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 commonSense

Reputation: 0
• Posts: 6
• Joined: 05-June 12

Posted 05 June 2012 - 06:02 PM

//Hi, I need to create a dice game for my computer science class and I ran into a problem. Basically, you roll five dice and I am trying to figure out if you scored doubles. I know I need to do an if statement. I tried something along the lines of

```if (dice1=dice2)
{
System.out.println("You rolled doubles!");
}
```

But, for this to work, I would have to repeat this step 25 times. I am wondering if there is an efficient way to do this. Your help would be greatly appreciated.

Jonah

Is This A Good Question/Topic? 0

## Replies To: Dice game task

### #2 Sheph

• D.I.C Lover

Reputation: 444
• Posts: 1,030
• Joined: 12-October 11

Posted 05 June 2012 - 06:17 PM

You have the right idea. For each dice you rolled, you have to check each other dice. But you're wrong about the 25 comparisons. The first dice only needs to be checked against 4 others, the next one against 3 others, then 2 then 1, then you know the last one isn't any of the other 4. So, in actuality, there are only 10 comparisons. With that little bit of knowledge, we have shorted our code dramatically.

We can shorten it further if we use an array, and have the computer do the looping instead of hand writing it. Have you learned about arrays? How about for loops?

### #3 commonSense

Reputation: 0
• Posts: 6
• Joined: 05-June 12

Posted 06 June 2012 - 03:55 PM

Thanks for replying! I can't believe I didn't see that earlier. And yes, that significantly shortens the code. I am comfortable with loops but I only need to do one dice roll. Would using a loop be beneficial? Thanks again for the help.

Jonah

### #4 Sheph

• D.I.C Lover

Reputation: 444
• Posts: 1,030
• Joined: 12-October 11

Posted 06 June 2012 - 06:16 PM

Yes a loop would save you from rewriting all of the code for each of the dice. (Notice the "for each" in that sentence. Usually sentences like that qualify for a loop) Imagine you have an array of 5 dice, and that you initialize them with 5 random numbers from 1-6.

```Random rand = new Random();
final int numberOfDice = 5;
int[] dice = new int[numberOfDice];

for(int i = 0; i < dice.length; i++) {
dice[i] = rand.nextInt(6) + 1; // 1-6
}
```

Based on the very little code you showed, I imagine this is already an improvement. You have variables called dice1, dice2 etc. That usually qualifies for an array. With an array we are able to shove repetitive code into a loop because they can be accessed simply by an index.

Now imagine you want to use 6 dice instead. All you have to do is change the numberOfDice variable, and you have more dice and you didn't have to rewrite any code. If you write the code correctly for verifying doubles, you won't have to change that either. Give it a try!