• 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

Can't find problem with my blackjack game

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Trying to create a javascript blackjack game. It is a simplified version with no suits, just number values. Whenever the player tries to hit it just repeats the beginning of my code. I think it might be something to do with my Do While loop, but i am unsure.

 
Saloon Keeper
Posts: 10732
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see you attempted to use Code tags. They weren't quite right so I fixed them, this time. For future reference you need to use the mouse to highlight then entire block of code and then click the Code button. It also helps to click the "Preview" button to make sure everything's ok before clicking "Submit".
 
Carey Brown
Saloon Keeper
Posts: 10732
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Seems like you may have several problems. For starters I would highly recommend
  • cleaning up your formatting. Lines 4 and 5 should start at the margin, and all other blocks should be indented by four spaces for each level of nesting.
  • Line 41 does not have any braces. It might be the way you want, but if so, your indents don't agree.
  • Line 29 you are checking the 'choice' variable, and line 44 you are assigning to the 'extracard' variable.
  •  
    Sheriff
    Posts: 67747
    173
    Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Joel Middleton wrote:Trying to create a javascript blackjack game.


    Your code is Java, not JavaScript. They are distinct and unrelated languages.
     
    Bear Bibeault
    Sheriff
    Posts: 67747
    173
    Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Carey Brown wrote:all other blocks should be indented by four spaces for each level of nesting.


    Or 2 (which seems to rapidly become the convention). As long is it's consistent.
     
    Rancher
    Posts: 517
    15
    Notepad Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hello Joel,

    I did some re-formatting your code. I also I tried compiling it. I ran into some issues.



    But, here is what I have done (I am referring to the code I have re-formatted above).
    1. I added braces on lines 28 and 39 (see comments at the lines)
    2. I removed closing brace on line 79

    Now this has one compiler error:

    JMProject3.java:45: error: variable extracard might not have been initialized
                   if (extracard.equals ("stay")) {
                       ^
    1 error


    You should be able to fix this error, by initializing the variable extracard before it is used. The code will compile without any errors (I haven't tried running it, yet).

    Now, is this of any help to you?
     
    Carey Brown
    Saloon Keeper
    Posts: 10732
    86
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
     
    J Hiddleston
    Greenhorn
    Posts: 25
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Prasad Saya wrote:Hello Joel,

    I did some re-formatting your code. I also I tried compiling it. I ran into some issues.



    But, here is what I have done (I am referring to the code I have re-formatted above).
    1. I added braces on lines 28 and 39 (see comments at the lines)
    2. I removed closing brace on line 79

    Now this has one compiler error:

    JMProject3.java:45: error: variable extracard might not have been initialized
                   if (extracard.equals ("stay")) {
                       ^
    1 error


    You should be able to fix this error, by initializing the variable extracard before it is used. The code will compile without any errors (I haven't tried running it, yet).

    Now, is this of any help to you?




    Hi! I greatly appreciate you fixing up my code for me! However, I tried to run the code that you fixed and  was able to get the variable extracard to initialise, however, whenever the user inputs 'hit' it does not give another card, it simply starts again from the beginning, and when the user types 'stay' it does what should happen when hit is received.
     
    Prasad Saya
    Rancher
    Posts: 517
    15
    Notepad Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Hi! I greatly appreciate you fixing up my code for me!



    You are welcome!

    What is it  that you want the program to do? Based on that (requirement) the program or code has to be written. Now, I don't know what this program is supposed to do; what it is about (Blackjack card game, yes, but how are you playing it in this program).

    For instance, what is the input from the user and the expected output. What are various scenarios? Once, you know all those scenarios you should be able to write or fix the code logic as you want it.

     
    Saloon Keeper
    Posts: 15524
    364
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Your first job is to remove all that code from the main() method, and create a bunch of methods that handle smaller tasks. Right now your game logic is mixed with display code. Java is an object oriented language, you should use classes to your advantage!
     
    Carey Brown
    Saloon Keeper
    Posts: 10732
    86
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I think enough time has gone by...so...
     
    Prasad Saya
    Rancher
    Posts: 517
    15
    Notepad Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Cool. I played to win and loose!

    I had to lookup Blakjack @ wikipedia, though.
     
    reply
      Bookmark Topic Watch Topic
    • New Topic