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 6 of 6

Thread: Java Nest Loop ( Dice Stimulation)

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

    Default Java Nest Loop ( Dice Stimulation)

    I'm currently creating a Dice Roll stimulation with nest loop, but my code seem kind of long, any idea on how I can shorten it?


    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);
            Random randNum = new Random();
     
            int match = 0; //Number of times sum of dice matches the current sum
            int die1, 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+" %");


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Java Nest Loop ( Dice Stimulation)

    Quick answer #1: use an array of ints rather than 22+ int variables. Then you can loop through the array with ease should you desire.

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

    Default Re: Java Nest Loop ( Dice Stimulation)

    Oh I forgot to include that I can't use array Thanks though!

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Java Nest Loop ( Dice Stimulation)

    Quote Originally Posted by maple1100 View Post
    Oh I forgot to include that I can't use array Thanks though!
    Well if you can't use other collections such as an ArrayList, I think that you're stuck with what you have. Bummer.

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

    Default Re: Java Nest Loop ( Dice Stimulation)

    Thank you . Also, looking at my code, if I wanted to do the number of sides base on the user input instead of 11, do you think there any way I can compare the sum of any given side then have a count on that? For example let say it I roll the dice and get 30 and 30 which would give me a sum of 60. In the code you see that I hardcore the if/else if to compare it to a number from 2-22 ( Because I hardcore in 11 sides so the max number of sum is 22). ** Sorry if question is unclear.

  6. #6
    Junior Member
    Join Date
    Dec 2012
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Java Nest Loop ( Dice Stimulation)

    First time helping, here we go!

    Arrays:
    Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)
    ArrayLists:
    10 example of using ArrayList in Java >>> Java ArrayList Tutorial

    That's all I can help you with, arrays are really simple and so are arraylists. Try them out!

    /Adzox

Similar Threads

  1. [SOLVED] Dice game need help
    By lf2killer in forum Loops & Control Statements
    Replies: 6
    Last Post: October 5th, 2012, 02:25 AM
  2. Major Help, SAVE ME!! Java Dice Game - Keep Score?
    By jbarcus81 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 4th, 2012, 06:36 AM
  3. Java Programming - Dice Game Help!
    By blackvelvet in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 19th, 2012, 11:47 AM
  4. Help with creating a dice rolling program in Java
    By lilmiss in forum Object Oriented Programming
    Replies: 4
    Last Post: October 26th, 2011, 09:27 PM
  5. Java Dice Program
    By ebone in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 22nd, 2011, 11:07 PM