Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2

Thread: For Loops Help

  1. #1
    New to the CF scene
    Join Date
    Aug 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    For Loops Help

    At the moment I am learning Java, and I am currently going through pretty much basics. I have an assignment that makes me practice for loops and random numbers. The assignment tells to write a program to simulate tossing a pair of 11-sided dice and determine the percentage of times each possible combination of the dice is rolled.

    I'm supposed to ask the user how many times to roll the dice, and from there calculate the probability of the sum to come up. For now, I'm doing it with a 4 sided dice. What I'm having a problem with is making the loops calculate the probability for the sums.

    This is what I have:
    Code:
            int rolls;
            int counter = 0;
            int dice1 = 0;
            int dice2 = 0;
            Scanner in = new Scanner(System.in);
            System.out.print("Number of rolls: ");
            rolls = in.nextInt();
            Random randNumList = new Random();
            timesRolled = 0;
            System.out.println();
            System.out.println("Times rolled: " + timesRolled);
            System.out.println("Sum of dice     Probability");
            
            for(timesRolled = 0; rolls > timesRolled; timesRolled++)
            {    
                dice1 = randNumList.nextInt(5);
                dice2 = randNumList.nextInt(5);
            
                    for(int diceSum = (dice1 + dice2); diceSum == 2; counter++)
                    {
                        double prob = (double)counter / 8;
                        System.out.println("    2s:              " + prob);
                    }
                    for(int diceSum = dice1 + dice2; diceSum == 3; counter++)
                    {
                        double prob = (double)counter / 8;
                        System.out.println("    3s:              " + prob);
                    }
                    for(int diceSum = dice1 + dice2; diceSum == 4; counter++)
                    {
                        double prob = (double)counter / 8;
                        System.out.println("    4s:              " + prob);
                    }
                    for(int diceSum = dice1 + dice2; diceSum == 5; counter++)
                    {
                        double prob = (double)counter / 8;
                        System.out.println("    5s:              " + prob);
                    }
                    for(int diceSum = dice1 + dice2; diceSum == 6; counter++)
                    {
                        double prob = (double)counter / 8;
                        System.out.println("    6s:              " + prob);
                    }
                    for(int diceSum = dice1 + dice2; diceSum == 7; counter++)
                    {
                        double prob = (double)counter / 8;
                        System.out.println("    7s:              " + prob);
                    }
                    for(int diceSum = dice1 + dice2; diceSum == 8; counter++)
                    {
                        double prob = (double)counter / 8;
                        System.out.println("    8s:              " + prob);
                    }
                
            }
    As you can see, it's very basic printing-to-terminal kind of stuff. When I run this, it doesn't work properly. Any help is appreciated. I've asked my teacher for help but all he does is give me hints, and its frustrating because I'm good with methods, but not with the math part.
    Thanks

  • #2
    Regular Coder
    Join Date
    May 2002
    Location
    Virginia, USA
    Posts
    620
    Thanks
    0
    Thanked 6 Times in 6 Posts
    I'm confused. Are you supposed to determine the probability of sums of the die or the percentage of sums actually rolled?


  •  

    Posting Permissions

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