PDA

View Full Version : Another Java Question.




Hylekmi
Feb 1, 2012, 08:44 PM
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.


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



mgkimsal
Feb 1, 2012, 09:47 PM
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.


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



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

Hylekmi
Feb 1, 2012, 10:11 PM
Wow. I wasn't very far off. Thanks A lot! I really learned a lot from this.

-mike

mgkimsal
Feb 1, 2012, 10:33 PM
Wow. I wasn't very far off. Thanks A lot! I really learned a lot from this.

-mike

I don't know it'll work 100% like that, but that's the gist of it. Glad it could help.

robbieduncan
Feb 2, 2012, 04:31 AM
[code] for (int counter=0; counter<=49; counter= counter++)
The last part of this is odd. Certainly not something you normally see done.

counter = counter++
is the same as
counter++

which is the normal thing to write.

wpotere
Feb 2, 2012, 04:51 AM
You could have also simply subtracted the headcount from 50 to get the tail count since there are only two possible outcomes. ;)

robbieduncan
Feb 2, 2012, 04:56 AM
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

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


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


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

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)

wpotere
Feb 2, 2012, 05:29 AM
Good points Robbie. I didn't go over the code very well, just a quick scan. :cool: