• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Do, while loops

 
John Vorona
Ranch Hand
Posts: 48
Java Mac OS X Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12186
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
John Vorona
Ranch Hand
Posts: 48
Java Mac OS X Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could I do this in one .java file, or would I need another class for this to work ?
 
John Vorona
Ranch Hand
Posts: 48
Java Mac OS X Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bump
 
Paul Clapham
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48
Java Mac OS X Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Paul.
 
John Vorona
Ranch Hand
Posts: 48
Java Mac OS X Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 49793
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic