
Nested Loops
Hi everybody I am really new to this so please bear with me. I take an online AP Computer Science class in java using BlueJ and the teacher isnt always readily available. Anyway, we have to create a program that simulates tossing a pair of dice and determines the percentage of times each possible combination of the dice is rolled. The user tells me how many sides the dice have and how many times to roll it. I have to use nested loops, and I haven't learned about arrays yet so I can't use them. The algorithm they provided for us is 1. Using nested loops, cycle through the possible sums of the dice. 2. Roll the dice the given number of times for each sum. 3. Count how many times the sum of the dice match the current sum being looked for.
Here is my code so far,
import java.util.Scanner;
import java.util.Random;
public class DiceProbability
{
public static void main (String [] args)
{
Scanner in = new Scanner(System.in);
Random randNum = new Random();
int trials = 0; // number of times the dice have been rolled
int sum;
int intRandSum = 0; //Represents the outcome of each roll of the die
System.out.print("How many sides do you want the die to have?: ");
int sides = in.nextInt();
System.out.print("How many times would you like the dice rolled?: ");
int rolls = in.nextInt();
System.out.println();
System.out.print("\tSum of Dice");
System.out.println("\t\tProbability");
for (int numberOfRolls = 1; numberOfRolls <= rolls; numberOfRolls ++)
{
int intRandNum = randNum.nextInt(sides) + 1; //this process simulates rolling the dice
int intRandNum2 = randNum.nextInt(sides) + + 1;
intRandSum = intRandNum + intRandNum2;
System.out.println(intRandSum);
}
for (sum = 2; sum <= (sides * 2); sum ++)//this process here simulates every possible sum of the dice
{
System.out.println("\t" + sum);
}
}
}
Um, I am far from being complete because I know this code doesn't solve the probability or anything like that. Basically, I am just very confused on how exactly I am supposed to "nest" these two loops, if indeed this are the two loops that need to be nested, and how exactly am I supposed to "count" how many times the sum of the dice matches the sum currently being looked for as stated in the algorithm. Any help would be greatly appreciated.

You already have the roll variable. Simply put another for loop inside your existing one that goes from 0 to roll1. If you need to only print out the information regarding the frequency of rolls, this pseudocode should do you nicely (note I am ommiting what you already have done regarding user input):
Code:
for each sum from 2 to sides*2
for each i from 0 to roll1
count = 0
produce 2 random numbers in [1,sides]
if sum of random numbers equal to sum
count++
print out count
If you need to store them, i don't see how to do it without arrays or some other data structure.