1. Member Join Date
Feb 2009
Posts
11
Rep Power
0

## 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
Java Code:
```/**
* Write a description of class DiceProbability here.
*
* @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);
}

}

}
}
}```  Reply With Quote

2. Senior Member Join Date
Sep 2008
Posts
564
Rep Power
13

## 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?  Reply With Quote

3. ## Just a tip to shorten the amount of lines:
Java 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:
Java 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  Reply With Quote

4. ## Take the following out of the "for" loop:
Java 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  Reply With Quote

5. Member Join Date
Feb 2009
Posts
11
Rep Power
0

## 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.  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•