Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Hylekmi

macrumors regular
Original poster
Dec 8, 2010
101
0
hey guys, I created this program to "flip a coin"(only 50 times) I have successfully flipped the coin fifty times but I need to output how many heads and how many tails there are in the 50 flips. I think that i need to make heads and tails two separate variables and then do some sort of Count. Im just not quite sure how to do it.

Heres my code.

Code:
public class coinFlip
{
	public static void main(String[]args)
	{
	
	for (int counter=0; counter<=49; counter= counter++)
	{
	int flip= (int) (Math.random()*2)+1;
	counter++;
	int tails = 2;
	int heads = 1;
	
		if(flip== heads)
		{ 
		System.out.println("Flip #"+counter+": Heads");
		}//end if
		if(flip== tails)
		{
		
		System.out.println("Flip #"+counter+": Tails");
		}//endif

	}//end do
	
	System.out.println("You rolled ");
	}//end main
}//end public class
 
Last edited by a moderator:
hey guys, I created this program to "flip a coin"(only 50 times) I have successfully flipped the coin fifty times but I need to output how many heads and how many tails there are in the 50 flips. I think that i need to make heads and tails two separate variables and then do some sort of Count. Im just not quite sure how to do it.

Heres my code.

Code:
public class coinFlip
{
	public static void main(String[]args)
	{
	
	for (int counter=0; counter<=49; counter= counter++)
	{
	int flip= (int) (Math.random()*2)+1;
	counter++;
	int tails = 2;
	int heads = 1;
	
		if(flip== heads)
		{ 
		System.out.println("Flip #"+counter+": Heads");
		}//end if
		if(flip== tails)
		{
		
		System.out.println("Flip #"+counter+": Tails");
		}//endif

	}//end do
	
	System.out.println("You rolled ");
	}//end main
}//end public class


Code:
public class coinFlip
{
	public static void main(String[]args)
	{
	int tailCount =0;
        int headCount =0;

	for (int counter=0; counter<=49; counter= counter++)
	{
	int flip= (int) (Math.random()*2)+1;
	counter++;
	int tails = 2;
	int heads = 1;
	
		if(flip== heads)
		{ 
headCount++;
		System.out.println("Flip #"+counter+": Heads");
		}//end if
		if(flip== tails)
		{
		tailCount++;
		System.out.println("Flip #"+counter+": Tails");
		}//endif

	}//end do
	
	System.out.println("You rolled " + headCount + " heads and " + tailCount + " tails");
	}//end main
}//end public class
 
Last edited by a moderator:
Wow. I wasn't very far off. Thanks A lot! I really learned a lot from this.

-mike
 
You could have also simply subtracted the headcount from 50 to get the tail count since there are only two possible outcomes. ;)
 
You could have also simply subtracted the headcount from 50 to get the tail count since there are only two possible outcomes. ;)

Indeed. Although the fact that

Code:
counter++

exists inside the loop as well as in the loop conditions means that the loop won't actually get executed 50 times. And on a stylistic note re-declaring the heads and tails constants every time round the loop is pretty weird. It'd be normal to put something like

Code:
private static final int TAILS = 2;

at the top of the class.

Finally checking the flip variable twice is pointless. If it's not heads it has to be tails so

Code:
int flip= (int) (Math.random()*2)+1;

if(flip== heads)
{ 
headcount++;
System.out.println("Flip #"+counter+": Heads");
}//end if
if(flip== tails)
{
tailcount++;
System.out.println("Flip #"+counter+": Tails");
}//endif
(note I have removed the counter++ which will make the loop logically incorrect and the unnecessary re-declaration of the constants)

could simply become
Code:
if(((int) (Math.random()*2)+1)== HEADS)
{ 
headcount++;
System.out.println("Flip #"+counter+": Heads");
}
else
{
tailcount++;
System.out.println("Flip #"+counter+": Tails");
}
(assuming a private static final declaration of the constant HEADS)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.