Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Head-Tail Counter

 
Nicole Doetsch
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to create a program that simulates flipping a coin. The directions my teacher gave me are as follows:

1. Program should prompt the user for number of runs
2. Program should prompt the user for the number of times to flip the coin in a single run
3. Limit the number of flips per run from 1 - 100,000
4. Limit the number of flips from 2 - 20
5. Program should display "H" for heads and "T" for tails for each individual toss off the coin
6. Program should display each run as a series of tosses
7. Program should display the total number of runs that had all HEADS and the total number of runs that had all TAILS
8. Program should display a message indicating a <<<ALL HEADS>>> or <<<ALL TAILS>> run

The example output should be like this (6 runs with 3 flips per run)

H T H
H T T
H H H <<ALL HEADS>>>
T T H
T H T
T T T <<ALL TAILS>>>

Number of runs with all heads: 1
Number of runs with all tails: 1

Below is my code so far. My main problem is I'm unsure how to make it stop and make a new line once the number of flips entered is reached. Right now all the code manages to do is print a giant loop of H T H T H T H T. . .



 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On line 21, you tell the loop to execute as long as it is true that runs is less than 100000 and flips is less than or equal to twenty.

Question: does anything ever happen in the code to make that condition false?
 
Nicole Doetsch
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dennis Deems wrote:On line 21, you tell the loop to execute as long as it is true that runs is less than 100000 and flips is less than or equal to twenty.

Question: does anything ever happen in the code to make that condition false?


How would you go about making the condition false? (give me an example)

Sorry, I'm really new to java.
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12098
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well...

What are 'runs' and 'flips' supposed to represent?

for example...suppose I wanted to flash a light 10 times...I might write a loop like this:




Each time i run through this loop, the flash() method is called, and the variable i is incremented. i is my counter of how many times i have run my loop. Eventually, 'i' will no longer be less than 10 (in fact, after exactly 10 executions of the loop), and the condition will be false.

You need to do something similar.

My guess is that you want to actually have two nested loops...one for runs, and one for flips. But to be honest, you need to back up before you do that. You're trying to do too much at once. You need to get your paper and pencil out and sketch out in ENGLISH what your program should do.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nicole Doetsch wrote:
How would you go about making the condition false? (give me an example)




That line says, "Keep going as long as the number of runs is 100000 or less and the number of flips is 20 or less". (I'm hoping you understand at least that much of what you wrote.)

Now, you don't need to know a single thing about Java to be able to describe when it will stop.

So, tell me, if we're going to keep going as long as runs is 100000 or less and flips is 20 or less, when will we stop?
 
Nicole Doetsch
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:well...

What are 'runs' and 'flips' supposed to represent?

for example...suppose I wanted to flash a light 10 times...I might write a loop like this:




Each time i run through this loop, the flash() method is called, and the variable i is incremented. i is my counter of how many times i have run my loop. Eventually, 'i' will no longer be less than 10 (in fact, after exactly 10 executions of the loop), and the condition will be false.

You need to do something similar.

My guess is that you want to actually have two nested loops...one for runs, and one for flips. But to be honest, you need to back up before you do that. You're trying to do too much at once. You need to get your paper and pencil out and sketch out in ENGLISH what your program should do.


Thanks! I'll try our your suggestion with the nested loops and see how it goes!
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nicole Doetsch wrote:
Thanks! I'll try our your suggestion with the nested loops and see how it goes!


Stop. Before you write a line of code, do you understand WHY nested loops? Do you get how that would relate to how you would write out the steps in English? Do you understand what each of the loops would represent, in terms of your problem statement?

Once you're confident that you do understand, write one loop first. It doesn't matter which one. Write it, code it, test it, get it working completely. Then do the other one, all by itself, without regard to the first one. Then, once they both work, put them together. (For the outer one, in the spot where the inner one would be, just put a place holder at first, like System.out.println("inner loop goes here");)

Also, be aware that your inner loop will be a good candidate for its own method. Or else just its body will. Either way is valid.
 
Nicole Doetsch
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it to work using a nested for loop! It took a lot of brainstorming, but then it him me that in order to loop it through the amount of times I wanted it to I'd need to set a separate accumulator. Then a simple if statement checks to see if the numbers entered are in the ranges my teacher wants.

Here's the code as it stands now:


The last things I need to do is make it count flips that contain all heads and all tails and print <<ALL HEADS>> or <<ALL TAILS>> on the line. However, I'm unsure which type of statement to use. Would a simple if...then statement work?
 
surendra thogata
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hope this will helpful:
 
Nicole Doetsch
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is quite an old post and I've been finished with that particular project long ago.
Here's my final code
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic