# Nested Loops

• 02-18-2009, 01:14 AM
ks1615
Nested Loops
Hello, I'm a new member of these forums.
My program (using BlueJ) is supposed to roll 2 11-sided (6 will work too) dice. But I ask the use how many times he/she wants to throw it. Then as it is thrown, depending on the sum of the 2 dice, the sum of one specific number (i.e die1=5, die2=7, total=12) gets a counter added meaning 12 gets 1/100. Then it is supposed to calculate the probabilty of the overall.
Also note, i cannot use arrays, the 2 rolls are generated using a random generator.
Here is my code
Code:

``` /**  * Write a description of class DiceProbability here.  *  * @author (your name)  * @version (a version number or a date)  */ import java.util.Scanner; import java.util.Random; public class DiceProbability {   public static void main(String[] args)   {       int totalThrows = 0;       Scanner in = new Scanner(System.in);       System.out.print("Number of rolls: ");       totalThrows = in.nextInt();             int sides = 11;             int totalMatches = 0;       for(int sum = 0; sum <= 2*sides; sum++)       {                          for(int roll = 0; roll < totalThrows; roll++)         {             Random randNumList = new Random ();             int randNum1 = randNumList.nextInt(12);             int randNum2 = randNumList.nextInt(12);             int number = randNum1 + randNum2;             if(number != 2)             {                 roll++;                 double probability =  roll / totalThrows;                 System.out.println("Probabilty of 2: " + probability);             }             else if(number != 3)             {                 roll++;                 double probability = roll / totalThrows;                 System.out.println("Probability of 3: " + probability);             }               else if(number != 4)             {                 roll++;                 double probability = roll / totalThrows;                 System.out.println("Probability of 4: " + probability);             }               else if(number != 5)             {                 roll++;                 double probability = roll / totalThrows;                 System.out.println("Probability of 5: " + probability);             }               else if(number != 6)             {                 roll++;                 double probability = roll / totalThrows;                 System.out.println("Probability of 6: " + probability);             }               else if(number != 7)             {                 roll++;                 double probability = roll / totalThrows;                 System.out.println("Probability of 7: " + probability);             }               else if(number != 8)             {                 roll++;                 double probability = roll / totalThrows;                 System.out.println("Probability of 8: " + probability);             }               else if(number != 9)             {                 roll++;                 double probability = roll / totalThrows;                 System.out.println("Probability of 9: " + probability);             }               else if(number != 10)             {                 roll++;                 double probability = roll / totalThrows;                 System.out.println("Probability of 10: " + probability);             }               else if(number != 11)             {                 roll++;                 double probability = roll / totalThrows;                 System.out.println("Probability of 11: " + probability);             }               else if(number != 12)             {                 roll++;                 double probability = roll / totalThrows;                 System.out.println("Probability of 12: " + probability);             }               else if(number != 13)             {                 roll++;                 double probability = roll / totalThrows;                 System.out.println("Probability: " + probability);             }               else if(number != 14)             {                 roll++;                 double probability = roll / totalThrows;                 System.out.println("Probability: " + probability);             }               else if(number != 15)             {                 roll++;                 double probability = roll / totalThrows;                 System.out.println("Probability: " + probability);             }               else if(number != 16)             {                 roll++;                 double probability = roll / totalThrows;                 System.out.println("Probability: " + probability);             }               else if(number != 17)             {                 roll++;                 double probability = roll / totalThrows;                 System.out.println("Probability: " + probability);             }               else if(number != 18)             {                 roll++;                 double probability = roll / totalThrows;                 System.out.println("Probability: " + probability);             }                     }             } }   }```
• 02-18-2009, 01:33 AM
emceenugget
so, what do you need help with? is something in your code behaving a certain way you don't want it to? is there something in specific you don't know how to implement?
• 02-18-2009, 01:53 AM
MK12
Just a tip to shorten the amount of lines:
Code:

```    else if(number != 16)         {             roll++;             double probability = roll / totalThrows;             System.out.println("Probability: " + probability);         }     else if(number != 17)         {               roll++;               double probability = roll / totalThrows;               System.out.println("Probability: " + probability);           }```
change to this:
Code:

```    else if(number != 16) [COLOR="Orange"]{[/COLOR]             roll++;             double probability = roll / totalThrows;             System.out.println("Probability: " + probability);     [COLOR="Green"]} else if(number != 17) {[/COLOR]               roll++;               double probability = roll / totalThrows;               System.out.println("Probability: " + probability);     }```
Note the orange: Don't put bracket on next line, note the green, put closing bracket and the next else-if on the same line.
-MK12
• 02-18-2009, 02:03 AM
CJSLMAN
Take the following out of the "for" loop:
Code:

`Random randNumList = new Random ();`
otherwise you won't get random numbers (it will always start with the same one). Place it right before the "for" loop.

Luck,
CJSL
• 02-18-2009, 03:48 AM
ks1615
Hi, thanks for the replies and tips.
What I need help with is, the random numbers generate only one time but I need to do that as many times as the user says.
Then each number has to match to one of the if/ else if statements and then a counter needs to be set up.