Jump to content
Sign in to follow this  
evanin

Java Coin Flip

Recommended Posts

Working on summer assignment for school.

"Simulate flipping a coin 100 times and count and output the final number of how many heads and tails occur. To do this you must create a 0 (heads) and 1(tails) randomly. Use

int coint;
coin =(int)(Math.random()*2)

 

Im a bit rusty from summer but here is what i have.

class programII 
{
 public void main(String[] args)
 {  
 	int tails = 0;
   int heads = 0;
   int x = 0;
  
           while(x >= 100)
           {int coin;
       	coin = (int)(Math.random()*2);
           x++;
           if(coin = 0)
       	heads++;
       	if(coin = 1)
       	tails++;                  
       	
       }
       System.out.println("heads = " + heads + " and tails = " + tails);
       }
       }
       

JCreator is saying incompatible types on the two "if" lines.

thanks.

Share this post


Link to post
Share on other sites

== not =

 

= is "assignment" and == is "comparison", so more like

 

if (coin == 1) {

}

 

I'm pretty sure you want the while loop < 100 not >= 100 too :P

 

while (x < 100) {

}

 

Your indentation is real nasty BTW :)

Share this post


Link to post
Share on other sites

Well, if I'm not mistaken, you have "while(x>= 100)" I think you need "while(x<=100)"

 

But, its been a while since I've done any java.

 

EDIT: what markie said too, i almost forgot about that

Edited by hockeyrcks9901

Share this post


Link to post
Share on other sites

haha.

i feel stupid.

thanks.

 

and I know indentation is nasty. Its only been like 2 months since ive had class for it, ive forgotten almost everything.

 

now on to local bookstore program! the joys of homework.

Share this post


Link to post
Share on other sites
haha.

i feel stupid.

thanks.

 

and I know indentation is nasty. Its only been like 2 months since ive had class for it, ive forgotten almost everything.

 

now on to local bookstore program! the joys of homework.

541048[/snapback]

 

lol fair play!

 

Also, you could use a for loop instead of a while loop :)

 

for (x = 0; x < 100; x++) {

}

Share this post


Link to post
Share on other sites

not really, both loops are easy, jsut when you KNOW it's going 100 time for loops are more natural :D and i seem to remember that EXACT program from AP computer science...

Share this post


Link to post
Share on other sites

yes it is for AP Computer Science

Edited by evanin

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...