File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Do, while loops Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Do, while loops" Watch "Do, while loops" New topic

Do, while loops

John Vorona
Ranch Hand

Joined: Sep 21, 2013
Posts: 48

This code generates a random subtraction equation and waits for user input. If input is correct answer it generates another equation until the user gets it wrong. When the user input is wrong, it displays the correct answer and it shows the score (which is the number of correct streaks the user went on before getting it wrong.) I am at the point where the the user gets the answer wrong and the program asks whether the user wants to play again. I am wondering how to make it so if the user types 'y' for yes, the whole program starts from the beginning. Else if he states 'n' for no, the program terminates. Below is my code:

Any tips ?

There are 10 types of people: those who understand binary, and those who do not understand it.
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11152

yes. don't put all your code in the main method.

create a method that plays the game (it would more or less have all the code you have written) - something like playOneGame();

Have another method that prompts for user input, and loops for as long as they say "y". inside the loop, call the method above. call it something like keepPlaying();

Have your main method call the keepPlaying method.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
John Vorona
Ranch Hand

Joined: Sep 21, 2013
Posts: 48

Could I do this in one .java file, or would I need another class for this to work ?
John Vorona
Ranch Hand

Joined: Sep 21, 2013
Posts: 48

Paul Clapham

Joined: Oct 14, 2005
Posts: 18541

You are allowed to have more than one method in a class.

Look around at other classes you may be familiar with and you'll notice that they have more than one method.
John Vorona
Ranch Hand

Joined: Sep 21, 2013
Posts: 48

Thanks Paul.
John Vorona
Ranch Hand

Joined: Sep 21, 2013
Posts: 48

I have finally figured out what I wanted to do and the program works as expected. Although, I would like an experienced pair of eyes to look over the program and tell me if I could improve anything, maybe i have more code than I really need. etc..
So if you see anything, let me know. If it looks good, let me know .

Here it is:

Campbell Ritchie

Joined: Oct 13, 2005
Posts: 37913
Except before main, where it is needed, remove all occurrences of the keyword static. You will of course have to run the main method slightly differently.
Never use == true or == false. That is poor style and error‑prone because you might write = instead. It is if (b)... or if (!b) ... where “b” means some boolean value or expression.
That method is too long; it ought to be divided into several smaller methods.
The code is only partially indented, and the indentation appears inconsistent. Always use 4 spaces per level (except when you break a single statement over multiple lines) and never use tabs. If you get a decent text editor, you can set it to convert tabs to spaces and set the indentation automatically.
I agree. Here's the link:
subject: Do, while loops
Similar Threads
Play again function help?
Unsure of how to propery loop my program
Trouble with Guessing Game
Need help ending number guessing game and asking if user wants to play again
Beginning "While" Program Help