Results 1 to 2 of 2

Thread: Nested Loops

  1. #1
    joemama is offline Member
    Join Date
    Jan 2011
    Posts
    1
    Rep Power
    0

    Default 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.

  2. #2
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    You already have the roll variable. Simply put another for loop inside your existing one that goes from 0 to roll-1. 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 roll-1
        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.
    Ever seen a dog chase its tail? Now that's an infinite loop.

Similar Threads

  1. Nested Loops
    By candygirl198827 in forum New To Java
    Replies: 38
    Last Post: 12-01-2010, 06:03 AM
  2. Nested for loops
    By luke in forum New To Java
    Replies: 23
    Last Post: 10-21-2010, 02:49 AM
  3. nested for loops
    By Implode in forum New To Java
    Replies: 4
    Last Post: 09-01-2009, 08:47 AM
  4. Nested Loops
    By ks1615 in forum New To Java
    Replies: 4
    Last Post: 02-18-2009, 02:48 AM
  5. Nested loops?
    By gabriel in forum New To Java
    Replies: 4
    Last Post: 08-06-2007, 04:51 PM

Posting Permissions

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