如何将数字输出为字符串/名称

时间:2014-11-05 23:35:47

标签: java string random int generator

我正在为 HiLo纸牌游戏编写代码,其中玩家必须猜测下一张牌是否更高更低相等 虽然对于数字1112131,我希望输出为 Jack 女王国王王牌

我已经制定了程序,指出它在int0之间返回一个随机的13(我仍然不知道我将如何写要使它成为int1之间的随机13的代码 如何设置,以便1112131数字显示为

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

依此类推,我已尝试if语句并将int更改为String,但这两种方法都没有奏效,而且对我而言,我无能为力字符串生成器......

这是我的代码,非常感谢任何帮助

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" );
    }


}

2 个答案:

答案 0 :(得分:0)

将currentCard更改为String。然后而不是

currentCard = nextCard;

DO

currentCard = Integer.toString(nextCard);

然后你可以执行if语句并为输出分配所需的字符串。

答案 1 :(得分:0)

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" );

对于随机生成器,您可以生成0到12之间,然后添加1:

nextCard = ACE + generator.nextInt(KING);