Enjoy an ad free experience by logging in. Not a member yet? Register.

Results 1 to 2 of 2
Thread: For Loops Help

08022008, 12:48 AM #1
 Join Date
 Aug 2008
 Posts
 1
 Thanks
 0
 Thanked 0 Times in 0 Posts
For Loops Help
At the moment I am learning Java, and I am currently going through pretty much basics. I have an assignment that makes me practice for loops and random numbers. The assignment tells 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.
I'm supposed to ask the user how many times to roll the dice, and from there calculate the probability of the sum to come up. For now, I'm doing it with a 4 sided dice. What I'm having a problem with is making the loops calculate the probability for the sums.
This is what I have:
Code:int rolls; int counter = 0; int dice1 = 0; int dice2 = 0; Scanner in = new Scanner(System.in); System.out.print("Number of rolls: "); rolls = in.nextInt(); Random randNumList = new Random(); timesRolled = 0; System.out.println(); System.out.println("Times rolled: " + timesRolled); System.out.println("Sum of dice Probability"); for(timesRolled = 0; rolls > timesRolled; timesRolled++) { dice1 = randNumList.nextInt(5); dice2 = randNumList.nextInt(5); for(int diceSum = (dice1 + dice2); diceSum == 2; counter++) { double prob = (double)counter / 8; System.out.println(" 2s: " + prob); } for(int diceSum = dice1 + dice2; diceSum == 3; counter++) { double prob = (double)counter / 8; System.out.println(" 3s: " + prob); } for(int diceSum = dice1 + dice2; diceSum == 4; counter++) { double prob = (double)counter / 8; System.out.println(" 4s: " + prob); } for(int diceSum = dice1 + dice2; diceSum == 5; counter++) { double prob = (double)counter / 8; System.out.println(" 5s: " + prob); } for(int diceSum = dice1 + dice2; diceSum == 6; counter++) { double prob = (double)counter / 8; System.out.println(" 6s: " + prob); } for(int diceSum = dice1 + dice2; diceSum == 7; counter++) { double prob = (double)counter / 8; System.out.println(" 7s: " + prob); } for(int diceSum = dice1 + dice2; diceSum == 8; counter++) { double prob = (double)counter / 8; System.out.println(" 8s: " + prob); } }
Thanks

08202008, 01:46 PM #2
 Join Date
 May 2002
 Location
 Virginia, USA
 Posts
 621
 Thanks
 0
 Thanked 6 Times in 6 Posts
I'm confused. Are you supposed to determine the probability of sums of the die or the percentage of sums actually rolled?