Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-02-2008, 12:48 AM   PM User | #1
cancam
New to the CF scene

 
Join Date: Aug 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
cancam is an unknown quantity at this point
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
cancam is offline   Reply With Quote
Old 08-20-2008, 01:46 PM   PM User | #2
Celtboy
Regular Coder

 
Join Date: May 2002
Location: Virginia, USA
Posts: 620
Thanks: 0
Thanked 6 Times in 6 Posts
Celtboy is an unknown quantity at this point
I'm confused. Are you supposed to determine the probability of sums of the die or the percentage of sums actually rolled?
Celtboy is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:48 AM.


Advertisement
Log in to turn off these ads.