Results 1 to 3 of 3
Thread: Using loops for dice probability
 10052014, 09:28 PM #1Member
 Join Date
 Oct 2014
 Posts
 2
 Rep Power
 0
Using loops for dice probability
Hi, I'm new to posting here so please forgive me if this is a simple solution/ has already been answered.
Anyway, I'm taking AP Computer Science with a virtual school and I'm having trouble with this assignment. The instructions are to "write a program to simulate tossing a pair of 11sided dice and determine the percentage of times each possible combination of the dice is rolled... then ask the user
to enter the number of sides on a die" The code is all provided here, I just have to put it in the right spots. (Side note, I cannot use arrays.) Here's the original file I was given to work with:
Java Code:import java.util.Random; import java.util.Scanner; public class DiceProbability { public static void main(String[] args) { //Declare and initialize variables and objects //Input: ask user for number of rolls and number of sides on a die //Print heading for output table //*************************************************************************************** //Using nested loops, cycle through the possible sums of the dice. //Roll the dice the given number of times for each sum. //Count how many times the sum of the dice match the current sum being looked for. //*************************************************************************************** //Loop to increment through the possible sums of the dice //Loop to throw dice given number of times //Randomly generate values for two dice //Check if the sum of dice is equal to the given sum //After all throws, calculate percentage of throws that resulted in the given sum //Print results // Use the code below to complete the program. All statements are complete and when placed in the proper order, the program will work! // Be sure to indent code properly and include documation for each section.. System.out.println(" " + sum + "\t\t" + probability ); System.out.print("Please enter the number of sides on a die: "); System.out.print("Please enter the number of rolls: "); System.out.print("\n Sum of dice \t Probability \n"); Scanner in = new Scanner(System.in); Random randNum = new Random(); probability = (double)match / numRolls * 100; numSides = in.nextInt(); numRolls = in.nextInt(); match++; match = 0; int sum = 0; int numSides = 0; int numRolls = 0; int match = 0; //Number of times sum of dice matches the current sum int die2 = 0; int die1 = 0; double probability = 0.0; die2 = randNum.nextInt( numSides ) +1; die1 = randNum.nextInt( numSides ) +1; if ( die1 + die2 == sum ) { } // end if for( sum = 2; sum <= (numSides * 2); sum++ ) { } //end for for( int roll = 0; roll < numRolls; roll++ ) { } //end for } //end main }//end class DiceProbability
Java Code:public class DiceProbability1 { public static void main(String[] args) { //Declare and initialize variables and objects int sum = 0; int numSides = 0; int numRolls = 0; int match = 0; //Number of times sum of dice matches the current sum int die2 = 0; int die1 = 0; double probability = 0.0; Scanner in = new Scanner(System.in); //Input: ask user for number of rolls and number of sides on a die System.out.print("Please enter the number of sides on a die: "); numSides = in.nextInt(); System.out.print("Please enter the number of rolls: "); numRolls = in.nextInt(); //Print heading for output table System.out.print("\n Sum of dice \t Probability \n"); //*************************************************************************************** //Using nested loops, cycle through the possible sums of the dice. //Roll the dice the given number of times for each sum. //Count how many times the sum of the dice match the current sum being looked for. //*************************************************************************************** //Loop to increment through the possible sums of the dice //Loop to throw dice given number of times //Randomly generate values for two dice //Check if the sum of dice is equal to the given sum //After all throws, calculate percentage of throws that resulted in the given sum //Print results Random randNum = new Random(); for( sum = 2; sum <= (numSides * 2); sum++ ) { match = 0; for( int roll = 0; roll < numRolls; roll++ ) { die1 = randNum.nextInt( numSides ) +1; die2 = randNum.nextInt( numSides ) +1; if ( die1 + die2 == sum ) { match++; } // end if probability = (double)match / numRolls * 100; System.out.println(" " + sum + "\t\t" + probability ); } //end for } //end for } //end main }//end class DiceProbability
Please enter the number of sides on a die: 6
Please enter the number of rolls: 2
Sum of dice Probability
2 0.0
2 0.0
3 0.0
3 0.0
4 0.0
4 0.0
5 0.0
5 0.0
6 50.0
6 50.0
7 0.0
7 0.0
Now I don't really understand the concept in the first place as the lesson was extremely uninformative, so I don't know what went wrong here. Please help! (By the way, I'm using BlueJ, not sure if that's relevant.)
 10062014, 09:40 AM #2
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,422
 Blog Entries
 7
 Rep Power
 28
Re: Using loops for dice probability
You're printing the sum and the probability at each and every roll; you don't want that; you want to print those numbers after all rolls are done (hint: move lines #50 and $51 after line #52).
kind regards,
Jos
ps. and increase the number of rolls for better accuracy.Build a wall around Donald Trump; I'll pay for it.
 10072014, 01:17 AM #3Member
 Join Date
 Oct 2014
 Posts
 2
 Rep Power
 0
Similar Threads

New to Java please help!!! arrays loops while loops
By Silver22 in forum New To JavaReplies: 3Last Post: 05292014, 02:11 AM 
How to put dice on a JPanel?
By Ivan in forum New To JavaReplies: 4Last Post: 09132013, 06:47 PM 
probability sum of 2 cubes
By Mapisto in forum New To JavaReplies: 6Last Post: 12012011, 11:28 PM 
[SemiBeginner] (nested loops) What's wrong with my code? (nested loops)
By Solarsonic in forum New To JavaReplies: 20Last Post: 03222011, 05:02 AM 
How to generate bigram/trigram/unigram probability?
By findjoe in forum LuceneReplies: 4Last Post: 10042010, 09:01 AM
Bookmarks