• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Do, while loops

 
Ranch Hand
Posts: 48
Mac OS X Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac OS X Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac OS X Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
bump
 
Marshal
Posts: 28425
102
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac OS X Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Paul.
 
John Vorona
Ranch Hand
Posts: 48
Mac OS X Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

 
Marshal
Posts: 80874
506
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic