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

Thread: Question about my coin toss program

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    15
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Question about my coin toss program

    I made a coin toss program and it shows me the number of heads and tails that are generated by the program. My question is how do I get the program to show me just the percentage of heads and tails that are generated?

    import javax.swing.JOptionPane;
    public class Flip {
    public static void main(String args[])
    {
     
    int heads= 0;
    int tails= 0;
    	for(int i=0;i<26;i++){
     
    		double outcome =Math.random();
    		if(outcome >= 0.5)
    			heads++;
    		else
    			if (outcome < 0.5)
    				tails++;
     
    	}
     
    	JOptionPane.showMessageDialog(null, "Heads"+" "+heads);
    	JOptionPane.showMessageDialog(null, "Tails"+" "+tails);
     
    }
     
    }


  2. #2
    Member
    Join Date
    Jan 2010
    Location
    Oxford, UK
    Posts
    30
    Thanks
    2
    Thanked 7 Times in 7 Posts

    Default Re: Question about my coin toss program

    Your question is not very clear. Are you having trouble working out how to calculate the percentage, how to display it, or both? Or do you mean something else?

    If there are N trials, and heads of them come up heads, the percentage is
    double pctHeads = (double)heads*100/N
    The type cast to double is so you can get more precision than just the nearest percentage, if you so wish. No more parentheses are needed, due to the rules of operator precendence, but in case you're not sure, the following is equivalent
    double pctHeads = (((double)heads)*100)/N

    As for displaying it in a sensible way, you need to check out the Formatter class, which mimicks printf of C:
    Formatter (Java 2 Platform SE 5.0)

    I've never actually used this before; your post made me realise that I've never worried about formatted output in Java!

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

    CheekySpoon (February 8th, 2010)

  4. #3
    Junior Member
    Join Date
    Feb 2010
    Posts
    15
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Question about my coin toss program

    You made me realize I was doing something wrong and that combined by just walking away from it for a while helped me figure it out. Thanks!

  5. #4
    Member
    Join Date
    Jan 2010
    Location
    Oxford, UK
    Posts
    30
    Thanks
    2
    Thanked 7 Times in 7 Posts

    Default Re: Question about my coin toss program

    It's amazing how walking away from a problem and coming back later can make such a difference!
    Just yesterday I spent ages trying to write a simple algorithm (in Mathematica, not Java), but couldn't make it work. I got fed up, went home, and when I went back to the problem last night, solved it in about twenty minutes.

    I'm glad you sorted out your program.

Similar Threads

  1. [SOLVED] How to narrow down the range of input in java?
    By big_c in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 20th, 2009, 11:38 AM