Results 1 to 5 of 5
Thread: Nested Loops
 02182009, 01:14 AM #1Member
 Join Date
 Feb 2009
 Posts
 11
 Rep Power
 0
Nested Loops
Hello, I'm a new member of these forums.
My program (using BlueJ) is supposed to roll 2 11sided (6 will work too) dice. But I ask the use how many times he/she wants to throw it. Then as it is thrown, depending on the sum of the 2 dice, the sum of one specific number (i.e die1=5, die2=7, total=12) gets a counter added meaning 12 gets 1/100. Then it is supposed to calculate the probabilty of the overall.
Also note, i cannot use arrays, the 2 rolls are generated using a random generator.
Here is my code
Java Code:/** * Write a description of class DiceProbability here. * * @author (your name) * @version (a version number or a date) */ import java.util.Scanner; import java.util.Random; public class DiceProbability { public static void main(String[] args) { int totalThrows = 0; Scanner in = new Scanner(System.in); System.out.print("Number of rolls: "); totalThrows = in.nextInt(); int sides = 11; int totalMatches = 0; for(int sum = 0; sum <= 2*sides; sum++) { for(int roll = 0; roll < totalThrows; roll++) { Random randNumList = new Random (); int randNum1 = randNumList.nextInt(12); int randNum2 = randNumList.nextInt(12); int number = randNum1 + randNum2; if(number != 2) { roll++; double probability = roll / totalThrows; System.out.println("Probabilty of 2: " + probability); } else if(number != 3) { roll++; double probability = roll / totalThrows; System.out.println("Probability of 3: " + probability); } else if(number != 4) { roll++; double probability = roll / totalThrows; System.out.println("Probability of 4: " + probability); } else if(number != 5) { roll++; double probability = roll / totalThrows; System.out.println("Probability of 5: " + probability); } else if(number != 6) { roll++; double probability = roll / totalThrows; System.out.println("Probability of 6: " + probability); } else if(number != 7) { roll++; double probability = roll / totalThrows; System.out.println("Probability of 7: " + probability); } else if(number != 8) { roll++; double probability = roll / totalThrows; System.out.println("Probability of 8: " + probability); } else if(number != 9) { roll++; double probability = roll / totalThrows; System.out.println("Probability of 9: " + probability); } else if(number != 10) { roll++; double probability = roll / totalThrows; System.out.println("Probability of 10: " + probability); } else if(number != 11) { roll++; double probability = roll / totalThrows; System.out.println("Probability of 11: " + probability); } else if(number != 12) { roll++; double probability = roll / totalThrows; System.out.println("Probability of 12: " + probability); } else if(number != 13) { roll++; double probability = roll / totalThrows; System.out.println("Probability: " + probability); } else if(number != 14) { roll++; double probability = roll / totalThrows; System.out.println("Probability: " + probability); } else if(number != 15) { roll++; double probability = roll / totalThrows; System.out.println("Probability: " + probability); } else if(number != 16) { roll++; double probability = roll / totalThrows; System.out.println("Probability: " + probability); } else if(number != 17) { roll++; double probability = roll / totalThrows; System.out.println("Probability: " + probability); } else if(number != 18) { roll++; double probability = roll / totalThrows; System.out.println("Probability: " + probability); } } } } }
 02182009, 01:33 AM #2Senior Member
 Join Date
 Sep 2008
 Posts
 564
 Rep Power
 8
so, what do you need help with? is something in your code behaving a certain way you don't want it to? is there something in specific you don't know how to implement?
 02182009, 01:53 AM #3
Just a tip to shorten the amount of lines:
Java Code:else if(number != 16) { roll++; double probability = roll / totalThrows; System.out.println("Probability: " + probability); } else if(number != 17) { roll++; double probability = roll / totalThrows; System.out.println("Probability: " + probability); }
Java Code:else if(number != 16) [COLOR="Orange"]{[/COLOR] roll++; double probability = roll / totalThrows; System.out.println("Probability: " + probability); [COLOR="Green"]} else if(number != 17) {[/COLOR] roll++; double probability = roll / totalThrows; System.out.println("Probability: " + probability); }
MK12Tell me if you want a cool Java logo avatar like mine and I'll make you one.
 02182009, 02:03 AM #4
Take the following out of the "for" loop:
Java Code:Random randNumList = new Random ();
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
 02182009, 03:48 AM #5Member
 Join Date
 Feb 2009
 Posts
 11
 Rep Power
 0
Similar Threads

Nested Classes
By new.guy in forum New To JavaReplies: 13Last Post: 09072008, 04:44 AM 
Nested(Inner) Classes
By Z.S.Tehrani in forum New To JavaReplies: 7Last Post: 08132008, 10:54 AM 
Nested loops?
By gabriel in forum New To JavaReplies: 4Last Post: 08062007, 04:51 PM 
Nested For Loop
By yuchuang in forum New To JavaReplies: 1Last Post: 07082007, 01:11 PM 
Nested Tags JSP
By Marcus in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 06252007, 05:42 AM
Bookmarks