Results 1 to 2 of 2
Thread: Nested Loops
 01012011, 10:04 PM #1Member
 Join Date
 Jan 2011
 Posts
 1
 Rep Power
 0
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.
 01012011, 10:17 PM #2Senior Member
 Join Date
 Feb 2010
 Location
 Ljubljana, Slovenia
 Posts
 470
 Rep Power
 8
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):
Java 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
Ever seen a dog chase its tail? Now that's an infinite loop.
Similar Threads

Nested Loops
By candygirl198827 in forum New To JavaReplies: 38Last Post: 12012010, 07:03 AM 
Nested for loops
By luke in forum New To JavaReplies: 23Last Post: 10212010, 02:49 AM 
nested for loops
By Implode in forum New To JavaReplies: 4Last Post: 09012009, 08:47 AM 
Nested Loops
By ks1615 in forum New To JavaReplies: 4Last Post: 02182009, 03:48 AM 
Nested loops?
By gabriel in forum New To JavaReplies: 4Last Post: 08062007, 04:51 PM
Bookmarks