This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

coin flip trouble!!

 
nick sutton
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm working on a program that flips two coins and returns only the value of heads. I want it to loop only till it reaches a maximum amount of heads in a row. I dont think it works right. any help would be nice.
 
Janeice DelVecchio
Saloon Keeper
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's okay to post the code you have....... it'll be easier to see what's going on (or not going on). You'll probably get more responses too....


Janeice
 
Ruben Guillen
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Nick

Please give a look to class Random, it is related to half the solution of your problem. method nextInt(int n) will allow to simulate the throw of coins.

Then give a look to for statement. It completes the other half part of the solution.

Regards.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch

I see these two chaps (both assets to the ranch ( )) have beaten me to it with useful advice. The link to Random, however, is easier to find here. Remember to write "import java.util.Random;" before you write "public class Foo" otherwise the compiler can't find Random.
 
nick sutton
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here is the code:


 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use the CODE button; I have edited your post and you can see how much easier it is to read.

You appear to have two loops inside each other. Why?
You also have code you aren't using; you don't use the ready String, nor the code String, for example.
I presume your Coin#flip() method simply takes a random number and gives heads 50% of the time or tails 50% of the time.

Your main method (which is, by the way, too long) doesn't seem to count until you get 3 consecutive heads. You don't seem to count anything consecutive, only the total of heads. And you don't seem to stop when you get anything from that count.
 
nick sutton
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
does this look any better? I cleaned it up a little. I'm still having trouble looping it until one hits a sum of 3heads in a row. I put loops together to try and achieve a way to add up the heads count! thanks for cleaning it up.

 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it doesn't say anything in the code about 3. I can't understand what the code does.
Write down exactly what you want the code to do, and then write how you are going to tell the computer, in words of one syllable, how to achieve that. Your vocabulary will include things like "if you have not yet thrown three heads in a row do this, else do that," or "count the heads you have thrown since you last threw a tails," or "go back, pick up the coin and throw it once more."
Write it on paper, and keep making it simpler and simpler. Smaller and smaller words. Smaller and smaller steps.


If you keep going until you hit 3 consecutive heads, do you need a maximum number of throws at all?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what do the variables maxHeads and heads mean? Consider whether renaming them would make the code easier to understand.
 
nick sutton
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm starting to understand your point. sorry for causing any frustration on your end. I eliminated more from the code. I had a lot of unnecessary code. you were right about the flips command. why would i need that if there is a goal. lol. I think im over thinking this one. just trying to transition from c++ into java. I've just been a little rusty! I'm just confused by my flip commands. I also have another bit of code that also determines the flip function. thanks for all the help thus far!
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try putting counts of consecutive heads and tails into the Coin class, with "get" methods for both. That would be the object-oriented way, to make each class take are of itself. Use the Coin class from the main method.

When you do that, you can reduce that main method by something like 50%.

Beware of thinking C++ and Java are similar. They only appear to be similar. There are all sorts of nasty surprises waiting for people who think they are versions of the same language, but that has been discussed recently; if you care to look through the contents of this forum for the last few days you will find at least two threads.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And when you print out "coin 1 won" or " coin 2 won" remember there is still the possibility of a draw.
 
nick sutton
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cool thanks. I got the program running. It goes back and forth between the two coins winning and yes there is a draw once and a while! thanks for the heads up. shouldn't be to hard to fix.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done
Please show us what you have got (so your classmates can copy it )
 
nick sutton
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hah!! yah right! okay here it is:
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Touché!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic