# Thread: Random coin flip application

1. Member
Join Date
Oct 2009
Posts
20
Rep Power
0

## 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 countTails = 0;
int percentTails;

//Loop
for(int x = 0; x <= 10;x = x++)
{
x = x + 1;
//flip
flip = Math.random();
//if statement
if( flip <= .5)
{
}
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-17-2009 at 11:03 PM. Reason: Code tags added

2. Senior Member
Join Date
Aug 2009
Posts
294
Rep Power
0
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. Senior Member
Join Date
Aug 2009
Posts
294
Rep Power
0
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. 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. Member
Join Date
Oct 2009
Posts
20
Rep Power
0
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. Member
Join Date
Oct 2009
Posts
20
Rep Power
0
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. Member
Join Date
Oct 2009
Posts
20
Rep Power
0
It is outside it???

8. Member
Join Date
Oct 2009
Posts
20
Rep Power
0
wow, yeah...i copy and pasted but didnt remove the first set. Yep, you are definitely right.

Thank you!

#### Posting Permissions

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