简体   繁体   中英

how to output numbers as strings/names

I'm writing the code for a HiLo card game in which the player has to guess whether the next card drawn will be higher , lower or equal .
Although for the numbers 11 , 12 , 13 and 1 , I'd like the output to be Jack , Queen , King and Ace .

I've worked out the program to point where it returns a random int between 0 and 13 (I'm still unaware as to how I would write the code to make it only choose random int between 1 and 13 ).
How do I set it so the 11 , 12 , 13 and 1 numbers appear as

The Card pulled is the Ace, 
is the next card Higher, Lower or Equal?

and so on, I've tried if statements and changing int to String , but neither work, and to my avail was nothing I was able to find about a String generator...

Here is my code, any help would be greatly appreciated

import java.util.Random;
import javax.swing.JOptionPane;

public class HiLo {

    public static final int JACK = 11;
    public static final int QUEEN = 12;
    public static final int KING = 13;
    public static final int ACE = 1;

    public static void main(String[] args) {

        int correctGuesses = 0;

        Random generator = new Random();
        int currentCard;
        int nextCard = generator.nextInt( KING+1 );

        while (correctGuesses < 4) {
            currentCard = nextCard;
            nextCard = generator.nextInt( KING+1 );

            Object[] options = {"Higher",
                "Lower",
            "Equal"};
            int Input = JOptionPane.showOptionDialog(null,
            "The Card pulled is the " + currentCard +
            " \nis the next card Higher, Lower or Equal?",
            "HiLo Card Game",
            JOptionPane.YES_NO_CANCEL_OPTION,
            JOptionPane.QUESTION_MESSAGE,
            null, options, options[0]);

            if(nextCard > currentCard) {
                switch(Input) {
                    case JOptionPane.YES_OPTION:
                        correctGuesses++;
                        break;
                    case JOptionPane.NO_OPTION:
                    case JOptionPane.CANCEL_OPTION:
                        correctGuesses = 0;
                        break;
                }
            } else if(nextCard < currentCard) {
                switch(Input) {
                    case JOptionPane.NO_OPTION:
                        correctGuesses++;
                        break;
                    case JOptionPane.YES_OPTION:
                    case JOptionPane.CANCEL_OPTION:
                        correctGuesses = 0;
                        break;
                }
            } else {
                switch(Input) {
                    case JOptionPane.CANCEL_OPTION:
                        correctGuesses++;
                        break;
                    case JOptionPane.YES_OPTION:
                    case JOptionPane.NO_OPTION:
                        correctGuesses = 0;
                        break;
                }
            }
        }

        JOptionPane.showMessageDialog(null,  
            "Congratulations, You guessed correctly 4 times,"
        + "\nthe Last Card was the " + nextCard + ", resart to play again" );
    }


}

Change currentCard to a String. Then instead of

currentCard = nextCard;

do

currentCard = Integer.toString(nextCard);

Then you can do your if statements and assign the strings you need to for the output.

String getCardString(int card) {

    String cardString = null;

    switch (card) {
        ACE:
            cardString = "ace";
            break;
        KING:
            cardString = "king";
            break;
        // same for queen and jack
        DEFAULT:
            cardString = Integer.toString(nextCard);
    }
       return cardString;
}


JOptionPane.showMessageDialog(null,  
            "Congratulations, You guessed correctly 4 times,"
        + "\nthe Last Card was the " + getCardString(nextCard) + ", resart to play again" );

For the random generator, you can generate between 0 and 12, then add 1:

nextCard = ACE + generator.nextInt(KING);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM