如何让我的程序将整数显示为字符串?

时间:2014-11-11 22:31:35

标签: java string integer

` 该程序会生成一张随机卡,您必须猜测生成的下一张卡是高于还是低于或等于 目前只输出数字,而不是卡片的名称 我希望它在整数" currentCard"时显示卡片的名称。和" nextCard"对应于1(ace),11(jack),12(queen)和13(king)的值

    Random  generator = new Random();
    int currentCard = generator.nextInt((KING-ACE)+1)+ACE;
    generator = new Random();
//ace = 1, king = 13, queen = 12, jack = 11
    while(count<GUESSES_TO_WIN)

    {

        int nextCard = generator.nextInt((KING-ACE)+1)+ACE;
        //here i want it to display "king" if the number generated is 13, "ace" if its 1,etc
                 String inputGuess = JOptionPane.showInputDialog(null,               
                "Your current card is " +currentCard + ". " +
                "Guess if the next card is higher, lower or equal to your current card."
                +"\n" + (4-count) + " more guesses until you win!");
        Scanner inputScanner = new Scanner (inputGuess);
        guess = inputScanner.nextLine();
        if(guess.equalsIgnoreCase("higher")&&
                (currentCard<nextCard))

        {
            JOptionPane.showMessageDialog(null,
                    guess +" is correct. The next card was " + nextCard);
            inputScanner.close();
            currentCard = nextCard;
            count++;
        }
        else if (guess.equalsIgnoreCase("lower") &&
                (currentCard>nextCard))
        {
            JOptionPane.showMessageDialog(null,
                    guess +" is correct. The next card was " + nextCard);
            inputScanner.close();
            currentCard = nextCard;
            count++;
        }
        else  if (guess.equalsIgnoreCase("equal to") &&
        (currentCard == nextCard))
        {
            JOptionPane.showMessageDialog(null,
                    guess +" is correct. The next card was " + nextCard);
            inputScanner.close();
            currentCard = nextCard;
            count++;`

目前只输出数字,而不是卡片的名称

1 个答案:

答案 0 :(得分:1)

使用枚举来表示一组扑克牌和用于命名映射的数字。请参阅Enum上的Javadocs:https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html

相关问题