StockAZ89
StockAZ89

Reputation: 51

Convert a random number to a string using switch statements

I'm trying to write a program that generates a random number 1-4, and then using a switch statement, convert each number to a corresponding card suit (hearts-1, diamonds-2, clubs-3, spades-4).

Random rand = new Random();
        
int suit;
suit = rand.nextInt(4)+1;
        
switch(suit) {
    case 1:
        String hearts = "hearts";
        break;
    case 2:
        String diamonds = "diamonds";
        break;
    case 3:
        String clubs = "clubs";
        break;
    case 4:
        String spades = "spades";
        break;
}
        

When all that is done and runs, I still only get the numbers that were generated itself. Rather than getting the string "diamonds" if the number 2 was generated. My goal is to concatenate a random suit, and a random number to go with that suit at the end, so to my understanding, using System.out.println(); inside of each switch case is not what I want.

The assignment instructions are: "Design and implement an application called Cards that randomly selects a playing card. First use a random number generator (Random class) to create a number in the range 1 to 4. Then convert the number to a suit (heart, diamond, club, or spade) using a switch statement. Next, use the random generator to create a random number in the range 1 to 13. Convert the number to ace, 2, 3, etc using another switch statement. Display the suit and the value of the chosen card."

Upvotes: 1

Views: 865

Answers (2)

Alex
Alex

Reputation: 1

First, I see a logic error with the variables. I think you want one variables that is the card suit's name which can than take 4 different values.

Also, in order to have Aces, Jacks, Queens and Kings as well as 2s, and 3s, etc., you variable for card's number should be String as well. You did not mention any operation, so I am presuming this is ok.

I am beginning in Java, so I often have error with Random rand = new Random() and or Scanner sc = new Scanner(System.in) used multiple time with the same name. Therefore, I would create use Random with a different name for number and for suit.

import java.util.Random;

public class testRandom {

    public static void main(String[] args) {

        Random randSuit = new Random();
        Random randNumber = new Random();

        String suitName = "";
        String cardName = "";

        int suit;
        int cardNumber;

        suit = randSuit.nextInt(4)+1;
        cardNumber = randNumber.nextInt(13)+1;

        switch(suit) {
            case 1:
                suitName = "Hearts";
                break;
            case 2:
                suitName = "Diamonds";
                break;
            case 3:
                suitName = "Clubs";
                break;
            case 4:
                suitName = "Spades";
                break;
            }

        switch(cardNumber) {
            case 1:
                cardName = "Ace";
                break;
            case 2:
                cardName = "2";
                break;
            case 3:
                cardName = "3";
                break;
            case 4:
                cardName = "4";
                break;
            case 5:
                cardName = "5";
                break;
            case 6:
                cardName = "6";
                break;
            case 7:
                cardName = "7";
                break;
            case 8:
                cardName = "8";
                break;
            case 9:
                cardName = "9";
                break;
            case 10:
                cardName = "10";
                break;
            case 11:
                cardName = "Jack";
                break;
            case 12:
                cardName = "Queen";
                break;
            case 13:
                cardName = "King";
                break;
        }

        System.out.println("I got the " + cardName + " of " + suitName);

    }
}

Upvotes: 0

sazzad
sazzad

Reputation: 6267

You are creating a heart/a diamond/a club/a spade using the switch statement. But carefully read your instruction. It says, "convert the number to a suit". Let's create a suit, shall we?

Random rand = new Random();
        
int suitNumber = rand.nextInt(4)+1;
String suit = "";

switch(suitNumber) {
    case 1:
        suit = "hearts";
        break;
    case 2:
        suit = "diamonds";
        break;
    case 3:
        suit = "clubs";
        break;
    case 4:
        suit = "spades";
        break;
}

Upvotes: 2

Related Questions