-1

I'm making code for a Blackjack game.

I've already made the necessary switch statements to generate a random card and have set a variable to them so I don't have to keep copying and pasting.

My problem is that whenever I press run, it shows a zero and then the generated card. I know this is because I've set playerCard1 to zero, but I dunno how to get rid of it.

Here's my code if it's any help

public class var {

    public static void main(String[] args) {

        int playerCard1 = 0;
        int suit = 0;
        char suitChar = ' ';
        int num = playerCard1;

        //making playerCard1 generate a random number between 1 and 13
        playerCard1 = (int)(Math.random() * 13 + 1);

        //making suit generate a random number between 1 and 4 for first card
        suit = (int)(Math.random() * 4 + 1);

        System.out.println(num);

        //switch statements for suit of player's first card

        switch (suit){
            case 1: suitChar = 'D'; //diamonds
                break;

            case 2: suitChar = 'S'; //spades
                break;

            case 3: suitChar = 'H'; //hearts
                break;

            case 4: suitChar = 'C'; //clubs
                break;


        }//end of switch statements for suit of player's first card 
        switch (playerCard1){
        case 1: System.out.print("A" + suitChar);
            break;

        case 2: System.out.print("2" + suitChar );
            break;

        case 3: System.out.print("3" + suitChar);
        break;

        case 4: System.out.print("4" + suitChar );
            break;

        case 5: System.out.print("5" + suitChar );
            break;

        case 6: System.out.print("6" + suitChar );
            break;

        case 7: System.out.print("7" + suitChar);
            break;

        case 8: System.out.print("8" + suitChar );
            break;

        case 9: System.out.print("9" + suitChar);
            break;

        case 10: System.out.print("10" + suitChar );
            break;

        case 11: System.out.print("J" + suitChar );
            break;

        case 12: System.out.print("Q" + suitChar);
            break;

        case 13: System.out.print("K" + suitChar);
            break;


            }//end of switch statements for playerCard1


    }//end of main

}//end of class
Agnohendrix
  • 480
  • 1
  • 7
  • 17
Saar
  • 1
  • 3
  • `Math.random() * 13 + 1` is not an efficient way to generate a range of random values. Use `rand.nextInt((max - min) + 1) + min` instead [How to generate random integers within a specific range in Java?](https://stackoverflow.com/q/363681/995714). `Math.random()` also uses `rand.nextInt` internally – phuclv Nov 06 '18 at 17:49
  • System.out.println(num); is the variable made so I don't have to keep copying and pasting the same switch statement over and over – Saar Nov 06 '18 at 17:54

1 Answers1

0

Remove the line:

 System.out.println(num);

That is printing 0 when you don't want it to.

You could change it to:

 System.out.println(playerCard1);

To print the random number of the chosen card, if you want to see it.

ᴇʟᴇvᴀтᴇ
  • 12,285
  • 4
  • 43
  • 66