Results 1 to 5 of 5

Thread: Nested Loops

  1. #1
    ks1615 is offline Member
    Join Date
    Feb 2009
    Posts
    11
    Rep Power
    0

    Default Nested Loops

    Hello, I'm a new member of these forums.
    My program (using BlueJ) is supposed to roll 2 11-sided (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);
                }
                
            }
            
        }
    }
       }

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    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?

  3. #3
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    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);
              }
    change to this:
    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);
         }
    Note the orange: Don't put bracket on next line, note the green, put closing bracket and the next else-if on the same line.
    -MK12
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

  4. #4
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default

    Take the following out of the "for" loop:
    Java Code:
    Random randNumList = new Random ();
    otherwise you won't get random numbers (it will always start with the same one). Place it right before the "for" loop.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  5. #5
    ks1615 is offline Member
    Join Date
    Feb 2009
    Posts
    11
    Rep Power
    0

    Default

    Hi, thanks for the replies and tips.
    What I need help with is, the random numbers generate only one time but I need to do that as many times as the user says.
    Then each number has to match to one of the if/ else if statements and then a counter needs to be set up.

Similar Threads

  1. Nested Classes
    By new.guy in forum New To Java
    Replies: 13
    Last Post: 09-07-2008, 04:44 AM
  2. Nested(Inner) Classes
    By Z.S.Tehrani in forum New To Java
    Replies: 7
    Last Post: 08-13-2008, 10:54 AM
  3. Nested loops?
    By gabriel in forum New To Java
    Replies: 4
    Last Post: 08-06-2007, 04:51 PM
  4. Nested For Loop
    By yuchuang in forum New To Java
    Replies: 1
    Last Post: 07-08-2007, 01:11 PM
  5. Nested Tags JSP
    By Marcus in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 06-25-2007, 05:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •