Results 1 to 9 of 9
  1. #1
    Boomer1 is offline Member
    Join Date
    Oct 2009
    Posts
    20
    Rep Power
    0

    Default Random coin flip application

    Application for 10 random coin flips. At the end of the application I just want to display the percentage of the heads flipped and percentage of tails flipped. Can someone point me in a right direction please?

    Java Code:
    //import statements
    import java.util.*;
    
    public class FlipCoin
    {
    	public static void main(String [] args)
    	{
    		//declare variables
    	   double flip;
    		int count = 0;
    		int countHeads = 0;
    		int countTails = 0;
    		int percentHeads;
    		int percentTails;
    		
    		
    		//Loop
    		for(int x = 0; x <= 10;x = x++)
    		{
    			x = x + 1;
    		   //flip
    		   flip = Math.random();
    			//if statement
    		   if( flip <= .5)
    			{	
    				flip = countHeads;
    		    	countHeads = countHeads + 1;
    				percentHeads = countHeads * 10;
    				}
    			else
    			{			
    				flip = countTails;
    		      countTails = countTails + 1;
    				percentTails = countTails * 10;
    				System.out.println("Heads was flipped " + percentHeads + " percent of the time.");
    				System.out.println("Tails was flipped " + percentTails + " percent of the time.");
    
    		   
    			}
    		   
    			count = count +1;
    			
    						
    					
        	}//End Loop
    		
    		
    }//End Main
    }//End Class
    Last edited by Fubarable; 12-18-2009 at 12:03 AM. Reason: Code tags added

  2. #2
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    if you wana do this:
    x = x + 1;
    then do it like this instead:
    x += 1;
    or even better
    x++;
    It looks cleaner and do the same, But in this case, You shouldnt do this!
    In your for loop you got at the end "x++"
    This meens, it will increese the value of x with one everytime it has done one loop.
    So erase the whole x = x+1; thingy.

    And for the random your right now taking a random value with random amount of numbers.
    Do this:
    flip = Math.random(1); /this will give you a value between 0 and 1
    then ask if flip is < 0.5

    hope this helped

  3. #3
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    oh and put this:
    Java Code:
    System.out.println("Heads was flipped " + percentHeads + " percent of the time.");
    				System.out.println("Tails was flipped " + percentTails + " percent of the time.")
    ourside the for loop

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    If you want to print something out after the loop is done, then you'll want to have your println statements after the loop closes -- after your //End Loop comment.

    Also, you'll likely want to give your percent variables some initial value such as 0. Also, when calculating percent, you'll first calculate frequency which will be a floating point type value (double would work best here) and then change it to percent by mult by 100 and casting to int. One pitfall -- when you calculate the frequency, be sure you cast one of the numbers used in this equation (usually I use the numerator) to double when you divide. Otherwise you'll be doing int division: an int / int will return an int always -- and you don't want to do this. For instance 1/ 2 returns 0 (that's the double result rounded down to an int), but (double)1/2 returns 0.5.

    Edit: also please learn to use code tags as it makes your code much easier to read. I added them to your original post, but you should be doing this yourself. Please see my signature below to learn how to do this.

  5. #5
    Boomer1 is offline Member
    Join Date
    Oct 2009
    Posts
    20
    Rep Power
    0

    Default

    I see exactly what both of you were talking about. I will certainly learn to use code tags. Thanks for all your help, I appreciate it.

  6. #6
    Boomer1 is offline Member
    Join Date
    Oct 2009
    Posts
    20
    Rep Power
    0

    Default

    One more quick question, sorry...but how would I get the result to stop printing 20 times. What would I do if I wanted to just print the percentages of heads and tails?

    Thank you

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  8. #8
    Boomer1 is offline Member
    Join Date
    Oct 2009
    Posts
    20
    Rep Power
    0

    Default

    It is outside it???

  9. #9
    Boomer1 is offline Member
    Join Date
    Oct 2009
    Posts
    20
    Rep Power
    0

    Default

    wow, yeah...i copy and pasted but didnt remove the first set. Yep, you are definitely right.

    Thank you!

Similar Threads

  1. Java Coin Acceptor?
    By starzsimon in forum Advanced Java
    Replies: 5
    Last Post: 08-06-2011, 08:50 AM
  2. Replies: 14
    Last Post: 10-19-2009, 11:57 AM
  3. Replies: 8
    Last Post: 04-19-2009, 06:50 PM
  4. random numbers without random class`
    By carlos123 in forum New To Java
    Replies: 4
    Last Post: 01-17-2008, 11:44 PM
  5. Replies: 2
    Last Post: 11-11-2007, 09:07 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
  •