Another Java Question.

Discussion in 'Mac Programming' started by Hylekmi, Feb 1, 2012.

  1. Hylekmi, Feb 1, 2012
    Last edited by a moderator: Feb 2, 2012

    macrumors regular

    Joined:
    Dec 8, 2010
    #1
    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
     
  2. mgkimsal, Feb 1, 2012
    Last edited by a moderator: Feb 2, 2012

    macrumors member

    Joined:
    Jan 31, 2011
    #2

    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
     
  3. thread starter macrumors regular

    Joined:
    Dec 8, 2010
    #3
    Wow. I wasn't very far off. Thanks A lot! I really learned a lot from this.

    -mike
     
  4. macrumors member

    Joined:
    Jan 31, 2011
    #4
    I don't know it'll work 100% like that, but that's the gist of it. Glad it could help.
     
  5. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #5
     
  6. Guest

    Joined:
    Oct 7, 2010
    #6
    You could have also simply subtracted the headcount from 50 to get the tail count since there are only two possible outcomes. ;)
     
  7. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #7
    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)
     
  8. Guest

    Joined:
    Oct 7, 2010
    #8
    Good points Robbie. I didn't go over the code very well, just a quick scan. :cool:
     

Share This Page