# Java nested loop

• January 14th, 2013, 07:27 PM
maple1100
Java nested loop
Is there anyway I can shorten this code? It look long and kind of repetitive. I can't use array for this one. What the program does is a dice stimulation. Ask user for amount of roll ( it an 11 side dice). Int count from 1-22 is a counter keeping count of the sum of the dice each role.(There might be alot of magic number, sorry :( )Thanks
Code :

```import java.util.Random; import java.util.Scanner; public class DiceProbability { public static void main(String[] args) { //Declare and initialize variables and objects Scanner inRoll = new Scanner(System.in);   int match = 0; //Number of times sum of dice matches the current sum int die1; int die2; //Random generated numbers int nRoll ; int nSides = 11;   int totalCount = 0; int count2 = 0; int count3 = 0; int count4 = 0; int count5 = 0; int count6 = 0; int count7 = 0; int count8 = 0; int count9 = 0; int count10 = 0; int count11= 0; int count12= 0; int count13 = 0; int count14 = 0; int count15 = 0; int count16 = 0; int count17 = 0; int count18 = 0; int count19 = 0; int count20 = 0; int count21 = 0; int count22 = 0;   //Input: ask user for number of rolls and number of sides on a die System.out.println("Enter the number of rolls: "); nRoll = inRoll.nextInt();     //Print heading for output table System.out.println("Number of rolls: "+ nRoll);   //*************************************************************************************** //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 for(int sum = 2; sum <=2*nSides;sum++) { for(int roll = 0; roll < nRoll;roll++) { Random randNumList= new Random(); int randNum1 = randNumList.nextInt(nSides)+1; int randNum2 = randNumList.nextInt(nSides)+1; int sumOf = randNum1 + randNum2;   {totalCount++; if(randNum1 + randNum2 == 2) count2++; else if(sumOf ==3) count3++; else if(sumOf ==4) count4++; else if(sumOf ==5) count5++; else if(sumOf ==6) count6++; else if(sumOf ==7) count7++; else if(sumOf ==8) count8++; else if(sumOf ==9) count9++; else if(sumOf ==10) count10++; else if(sumOf ==11) count11++; else if(sumOf ==12) count12++; else if(sumOf ==13) count13++; else if(sumOf ==14) count14++; else if(sumOf ==15) count15++; else if(sumOf==16) count16++; else if(sumOf ==17) count17++; else if(sumOf ==18) count18++; else if(sumOf ==19) count19++; else if(sumOf ==20) count20++; else if(sumOf ==21) count21++; else count22++; } }   }   System.out.println("Sum of Dice:"+ "\t Probability"); System.out.println("2s:"+ " \t\t"+((double)count2/totalCount)*100+" %"); System.out.println("3s:"+ " \t\t"+((double)count3/totalCount)*100+" %"); System.out.println("4s:"+ " \t\t"+((double)count4/totalCount)*100+" %"); System.out.println("5s:"+ " \t\t"+((double)count5/totalCount)*100+" %"); System.out.println("6s:"+ " \t\t"+((double)count6/totalCount)*100+" %"); System.out.println("7s:"+ " \t\t"+((double)count7/totalCount)*100+" %"); System.out.println("8s:"+ " \t\t"+((double)count8/totalCount)*100+" %"); System.out.println("9s:"+ " \t\t"+((double)count9/totalCount)*100+" %"); System.out.println("10s:"+ " \t\t"+((double)count10/totalCount)*100+" %"); System.out.println("11:"+ " \t\t"+((double)count11/totalCount)*100+" %"); System.out.println("12:"+ " \t\t"+((double)count12/totalCount)*100+" %"); System.out.println("13:"+ " \t\t"+((double)count13/totalCount)*100+" %"); System.out.println("14:"+ " \t\t"+((double)count14/totalCount)*100+" %"); System.out.println("15:"+ " \t\t"+((double)count15/totalCount)*100+" %"); System.out.println("16:"+ " \t\t"+((double)count16/totalCount)*100+" %"); System.out.println("17:"+ " \t\t"+((double)count17/totalCount)*100+" %"); System.out.println("18:"+ " \t\t"+((double)count18/totalCount)*100+" %"); System.out.println("19:"+ " \t\t"+((double)count19/totalCount)*100+" %"); System.out.println("20:"+ " \t\t"+((double)count20/totalCount)*100+" %"); System.out.println("21:"+ " \t\t"+((double)count21/totalCount)*100+" %"); System.out.println("22:"+ " \t\t"+((double)count22/totalCount)*100+" %");   } //end main }//end class DiceProbability```
• January 14th, 2013, 08:20 PM
Norm
Re: Java nested loop
Quote:

can't use array for this one.
How many pieces of data does the program need to keep in memory?
Does it require one variable for each piece of data?

If you can't use an array, or any of the collection classes, then you'll need a variable for each piece of data you want to keep in memory.
• January 14th, 2013, 08:27 PM
helloworld922
Re: Java nested loop
Do you need to keep all the counts in memory at the same time?

If you don't, you can repeat the experiment for each possible outcome, printing out the results for one possible outcome as soon as you get it.