Flip Again?

In this program, you'll see how using a do-while loop might be better than a while loop.

Files Needed

What You Should See

The code I have provided does not compile. Once you fix it, it will look roughly like this.

You flip a coin and it is... TAILS
Would you like to flip again (y/n)? y
You flip a coin and it is... HEADS
Would you like to flip again (y/n)? y
You flip a coin and it is... HEADS
Would you like to flip again (y/n)? n

What You Should Do on Your Own

Assignments turned in without these things will receive no credit.

  1. The code as given does not compile. Notice that the while loop tests if again.equals("y"), but the variable again doesn't have a value at first. Give it a value so that the code will compile and the loop will run at least once.
  2. Now that program is working, change the loop from a while loop to a do-while loop. Make sure it still works.
  3. What happens if you delete what you added in step 1? Change the line back to just String again; Does the program still work? Why or why not? (Answer in a comment.)



©2013 Graham Mitchell

This assignment is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 United States License.
Creative Commons License