Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Homework help, blackjack game

1. ## Homework help, blackjack game

Hello, im trying to code a game of blackjack for a class assignment but it is very simple, just values 1-10 no suits or anything, just between the dealer and the player. You lose if you go over 21 or the dealer has a higher amount but still under 21. You win if you have a higher amount than the dealer but still under 21, or the dealer busts. Its mostly written out, but when I prompt the system to ask if you'd like to play again (y/n)? I type "y" and the program just automatically says I win, I'm not sure what I'm doing wrong. Any help would be greatly appreciated. Thank you.

PHP Code:
``` import java.util.Scanner;import java.util.*;public class Blackjack { public static void main(String[] args) {          String anotherCard, playAgain = "y";     int nextCard, card1, card2, dCard1, dCard2;     int cardTotal = 0, dTotal = 0;       Scanner keyboard = new Scanner(System.in);          Random random = new Random();          // Begin dealing the players first two cards          while ("y".equals(playAgain))     {         //dealers first two random cards         dCard1 = random.nextInt(10) + 1;         dCard2 = random.nextInt(10) + 1;                  //players first two random cards and card total         card1 = random.nextInt(10) + 1;         card2 = random.nextInt(10) + 1;         cardTotal = card1 + card2;                  //dealers two card total and display only one dealer card         dTotal = dCard1 + dCard2;         System.out.println("Dealer shows: " + dCard1);                  //display players first two cards & card total         System.out.println("First Cards: " + card1 + ", " + card2);         System.out.println("Total: "+ cardTotal);                  System.out.println("Another Card (y/n)?: ");         anotherCard = keyboard.nextLine();                  while ("y".equals(anotherCard))         {             nextCard = random.nextInt(10) + 1;             cardTotal += nextCard;             System.out.println("Card: " + nextCard);             System.out.println("Total: " + cardTotal);                          if (cardTotal > 21)             {             System.out.println("You busted, Dealer wins");             System.out.println("Do you want to play again? (y/n):");             playAgain = keyboard.nextLine();             }                          if (cardTotal < 21)             System.out.println("Another Card (y/n)?: ");             anotherCard = keyboard.nextLine();                         if ("n".equals(anotherCard))             System.out.println("Dealer had: " + dTotal);             System.out.println("You had: " + cardTotal);                                       while ("n".equals(anotherCard))             {                    if (dTotal < cardTotal && cardTotal < 21)                    {                     System.out.println("Yay you win!");                     System.out.println("Play again? (y/n): ");                     playAgain = keyboard.nextLine();                     if (playAgain.equalsIgnoreCase("y"))                         playAgain = "y";                                          if (dTotal > cardTotal && dTotal < 21)                         System.out.println("You lose!");                         playAgain = keyboard.nextLine();                      }                   }            }        }    }}  ```

2. Originally Posted by bluedude521
Hello, im trying to code a game of blackjack for a class assignment but it is very simple, just values 1-10 no suits or anything, just between the dealer and the player. You lose if you go over 21 or the dealer has a higher amount but still under 21. You win if you have a higher amount than the dealer but still under 21, or the dealer busts. Its mostly written out, but when I prompt the system to ask if you'd like to play again (y/n)? I type "y" and the program just automatically says I win, I'm not sure what I'm doing wrong. Any help would be greatly appreciated. Thank you.

PHP Code:
``` import java.util.Scanner; import java.util.*; public class Blackjack {  public static void main(String[] args) {            String anotherCard, playAgain = "y";      int nextCard, card1, card2, dCard1, dCard2;      int cardTotal = 0, dTotal = 0;         Scanner keyboard = new Scanner(System.in);            Random random = new Random();            // Begin dealing the players first two cards            while ("y".equals(playAgain))      {          //dealers first two random cards          dCard1 = random.nextInt(10) + 1;          dCard2 = random.nextInt(10) + 1;                    //players first two random cards and card total          card1 = random.nextInt(10) + 1;          card2 = random.nextInt(10) + 1;          cardTotal = card1 + card2;                    //dealers two card total and display only one dealer card          dTotal = dCard1 + dCard2;          System.out.println("Dealer shows: " + dCard1);                    //display players first two cards & card total          System.out.println("First Cards: " + card1 + ", " + card2);          System.out.println("Total: "+ cardTotal);                    System.out.println("Another Card (y/n)?: ");          anotherCard = keyboard.nextLine();                    while ("y".equals(anotherCard))          {              nextCard = random.nextInt(10) + 1;              cardTotal += nextCard;              System.out.println("Card: " + nextCard);              System.out.println("Total: " + cardTotal);                            if (cardTotal > 21)              {              System.out.println("You busted, Dealer wins");              System.out.println("Do you want to play again? (y/n):");              playAgain = keyboard.nextLine();              }                            if (cardTotal < 21)              System.out.println("Another Card (y/n)?: ");              anotherCard = keyboard.nextLine();                           if ("n".equals(anotherCard))              System.out.println("Dealer had: " + dTotal);              System.out.println("You had: " + cardTotal);                                          while ("n".equals(anotherCard))              {                     if (dTotal < cardTotal && cardTotal < 21)                     {                      System.out.println("Yay you win!");                      System.out.println("Play again? (y/n): ");                      playAgain = keyboard.nextLine();                      if (playAgain.equalsIgnoreCase("y"))                          playAgain = "y";                                            if (dTotal > cardTotal && dTotal < 21)                          System.out.println("You lose!");                          playAgain = keyboard.nextLine();                       }                    }             }         }     } }  ```
Just a couple of corrections needed. when importing
Code:
```import java.util.Scanner;
import java.util.*;```
There is no need to import the Scanner class because you import every class in the java.util package.

Also when dealing the cards, there is a small chance the player could get zero, so put the necessary error checking in there.

And finally to your main problem. (I think this might be it, i'm not sure )
Code:
```if (playAgain.equalsIgnoreCase("y"))
playAgain = "y";```
make sure to add and else if statement to check in case the user says no.

I'm not sure if I can find the error your seeing, so ask your teacher for help, and they should be able to help you more than I ever could