• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Stuck on being able to create a consistent quitting option in Java Guessing Game, Help:)!

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello! Any help so appreciated! I'm a beginner-beginner and I'm trying to create a High- Low Guessing Game in Java.
What it is supposed to have is: Players guess a number between 1 and 100, the game tells them if their guess is too high or too low, and when they guess right it tells them how many guesses they have made. It is also supposed to have an option to quit if they enter a sentinel (0 in this case), and tell them if their guess is out of the range of 1-100. So far I can make all of that work, BUT I can't make it so they can quit mid-game or that it tells them they are out of range unless both of these things happen in the first numbers they enter.
I realize it's probably something with how my loops are set up orrr.... I'm not sure, been reading forums and textbooks all week and still haven't quite figured it out, unfortunately.
Thank you so much in advance for any help!!

Here is the code I have so far:





 
Marshal
Posts: 4501
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would start by formatting the code.  If you are using an IDE this should be built-in, otherwise, there are several on-line formatters/beautifier such as this one.
 
Alida Thomas
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks! I'll do that right away too. We are working in bluej for this course at the moment.
 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Once you get into the while() loop on line 43 you are no longer checking for valid range of input or if it's equal to zero.
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note use of else-if
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you been taught to break your program into smaller, more manageable pieces through the use of methods? Having all your code mashed into a single method is not a good practice. It makes your code more complicated than it needs to be and inhibits testing and debugging.
 
Alida Thomas
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Carey Brown, thanks so much, that makes a lot of sense and I'm trying to play with that now within BlueJ. I've changed around the code so it's all within a while and with the boolean true or false. It won't quite compile yet to test it, but I'm getting there!

Junili Lacar, unfortunately, not yet, I looked ahead into that now though, it definitely feels like all a big blob of a mess when it's all together like this.
 
Alida Thomas
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I still have to put it through a more beautifying editor and clean it up with comments, etc, so it's not pretty, BUT IT WORKS!!! THANK YOU SO MUCH!!! Took me over a week which is sad I know, but learning so much! Thank you





 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do lines 74‑76 mean?
Why have you got System.exit() and break; so often. I am not convinced it is a good idea to use System.exit() at all.
Why do you think your random numbers will run from 0...100?
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic