Hi everyone! Don't judge my hotmess java code below... I'm a total newbie, and I'm trying! Despite that, I still struggle with syntax and also what to put where. Any help on my slot machine program below is appreciated! I keep getting erros related to the last bit of code or maybe with my y/n condition? Thanks for any help or advice!
or even better
or best yet ... you already have a while loop, just change that and do away with this if/exit entirely.
As a general rule you should never call exit() though there are cases where you have no choice.
Alison Gloekler wrote:I still struggle with ... what to put where.
Don't put everything in main(). As we say around here, MainIsAPain (←click on that, it's a link)
Putting all your code in main is like dumping all your clothes in one big drawer in your dresser. Organize your code into smaller chunks so it's easy to find where each thing is happening. This can be done through methods. Here's a better version of main() for your program:
This is what's called "composing" your method so that it doesn't have all the nitty-gritty details but instead gives the reader an idea of what the overall flow of the program is. All the code you wrote previously and jammed into one place would be tweaked apart and moved into the various methods that are being called here.
It's too bad that most students are taught about nitty-gritty details of various types of statements first before they're taught about organizing their code into logical chunks by defining different methods and composing the story of what's going to happen. If you want to make any progress, learn the latter before you focus on the former.
Junilu Lacar wrote:. . . It's too bad that most students are taught about . . . various types of statements first before they're taught about organizing their code into logical chunks . . .
Couldn't agree more (very underdone beef for that assertion). If somebody tells me, “We know how to write an enum but not a loop,” I would say that is the right way to teach object‑orientation. I would also like to know how much programming you can do without any control structures; the functional programming people would say you can program everything without control structures.
I like your use of random#nextInt(); I think it is far better than arithmetic with Math#random().
Have you considered other Scanner methods for continuing your loop?Of course, that requires you enter TRUE or false (case‑insensitive). You can obviate any exceptions with a loop:-That code is best as part of its own method, as Junilu implied, and in my opinion that method is best as part of a separate class, a utility class. If you do that, there is no need to check for equality to yes/no. It is possible to enhance that sort of code with a method to find the next boolean or yes or no.
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop