• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

So very close to having this slot machine program work!

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!


 
Saloon Keeper
Posts: 8750
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Line 101: if first test and second test are true, then the third test must be true. I think you meant to use "&&".

Line 106: Ok  but would have triggered 101 tests first. Think you need an a == 0 in line 101.

Line 111: Would have been triggered by 101.

Can you describe in English what the logic should be?

With your "cont" variable you need to use equals() to compare it not "==".
 
Carey Brown
Saloon Keeper
Posts: 8750
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, I think I got your logic wrong. Your problem is that these "if"s should be in reverse order. Do the test for triple first, then double, etc..

 
Carey Brown
Saloon Keeper
Posts: 8750
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you gotten to arrays yet? If so you could replace your switch()s with an array lookup like this.


 
Alison Gloekler
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your help! I am still learning so your advice is so helpful!  My only error now is for my If cont ==n issue.  I've tried all of the variations, and the one with the LEAST errors is




which gives this error:

error: ';' expected
          If cont equals ("n");
1 error
BUILD FAILED (total time: 0 seconds)

Any help is appreciated, as I am still learning syntax!

Thanks!
 
Carey Brown
Saloon Keeper
Posts: 8750
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 16669
278
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Marshal
Posts: 74375
334
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Campbell Ritchie
Marshal
Posts: 74375
334
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic