Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Nov 2016
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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 anotherCardplayAgain "y";
         
    int nextCardcard1card2dCard1dCard2;
         
    int cardTotal 0dTotal 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();  
                        }   
                    }
                }
            }
        }

    Last edited by VIPStephan; 11-08-2016 at 09:29 PM. Reason: added code BB tags

  2. #2
    New Coder
    Join Date
    Sep 2016
    Posts
    10
    Thanks
    0
    Thanked 1 Time in 1 Post
    Quote Originally Posted by bluedude521 View Post
    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 anotherCardplayAgain "y";
         
    int nextCardcard1card2dCard1dCard2;
         
    int cardTotal 0dTotal 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


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •