Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Java nested loop

  1. #1
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

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


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java nested loop

    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.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    maple1100 (January 14th, 2013)

  4. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

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

Similar Threads

  1. Please Help: Need Help with my Nested Loop
    By hiroprotagonist in forum Loops & Control Statements
    Replies: 1
    Last Post: November 24th, 2012, 10:43 AM
  2. Please Help: Need Help with my Nested Loop
    By hiroprotagonist in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 20th, 2012, 03:49 PM
  3. Nested for loop
    By wolf_fcknHaley33 in forum Loops & Control Statements
    Replies: 2
    Last Post: May 23rd, 2012, 08:49 AM
  4. Need help with java gui and nested for loop
    By gatorsgirl in forum Loops & Control Statements
    Replies: 11
    Last Post: March 24th, 2012, 12:35 PM
  5. nested loop
    By b109 in forum Java Theory & Questions
    Replies: 1
    Last Post: May 30th, 2010, 10:05 AM