如何在java中将数字设置为名称

时间:2014-11-02 22:19:12

标签: java computer-science

我试图设计一款可以播放Hi / Lo卡片游戏的游戏,直到用户连续获得4个正确的答案,虽然这是代码的问题..我不知道如何使它成为流行的卡号一旦用户声明"更高","更低"或"平等"是他们比较最后一张卡片的数字'号码到。

现在它将它与用户没有看到或未知的数字进行比较,因此他们不知道他们是对还是错。我知道我可以添加' nextCard'变量到showOptionDialog输出虽然我更喜欢输出一个数字,所以如果程序打印:

"The Card pulled is the 9
 Is the next card Higher, Lower or Equal?"

输出的下一个号码/卡是用户将之前的号码(9)与之比较的号码。

另外,

我已经设定了常数,但是我不知道如何制作它而不是打印11,12,13,1,它会打印JACK,QUEEN,KING,ACE,还有什么不打印。

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 && Input == JOptionPane.YES_OPTION )
            {
                correctGuesses++;
            }
            else if ( nextCard > currentCard && Input == JOptionPane.NO_OPTION )
            {
                correctGuesses = 0;
            }

            else if ( nextCard > currentCard && Input == JOptionPane.CANCEL_OPTION )
            {
                correctGuesses = 0;
            }

            else if ( nextCard < currentCard && Input == JOptionPane.YES_OPTION )
            {
                correctGuesses = 0;
            }

            else if ( nextCard < currentCard && Input == JOptionPane.NO_OPTION )
            {
                correctGuesses++;
            }

            else if ( nextCard < currentCard && Input == JOptionPane.CANCEL_OPTION )
            {   
                correctGuesses = 0;
            }

            else if ( nextCard == currentCard && Input == JOptionPane.YES_OPTION )
            {
                correctGuesses = 0;
            }

            else if ( nextCard == currentCard && Input == JOptionPane.NO_OPTION )
            {
                correctGuesses = 0;
            }

            else if ( nextCard == currentCard && Input == JOptionPane.CANCEL_OPTION )
            {
                correctGuesses++;
            }       
        }

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

}

2 个答案:

答案 0 :(得分:-1)

如果您希望变量不包含在单循环迭代中(并且您的问题希望您在两次迭代中使用nextCard值),则不要在循环内声明它。每次迭代也不需要新的生成器或选项对象。

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

对于卡片打印 - 你应该为卡片创建一个枚举,一个包含相关信息(值,套件)的枚举,以及覆盖负责打印的toString方法。写作应该足够简单。

答案 1 :(得分:-1)

嵌套,如果是凌乱的。你应该简化,这样你就不会一遍又一遍地重新评估同样的事情。例如,您的前三个图层评估nextCard > cardGenerated。如果将其解压缩到自己的if,代码将更具可读性。您还可以使用(Input == JOptionPane.XX_OPTION)

替换评估switch()的其他部分
        if(nextCard > cardGenerated)
        {
            switch(input)
            {
                case JOptionPane.YES_OPTION:
                    correctGuesses++;
                    break;
                case JOptionPane.NO_OPTION:
                case JOptionPane.CANCEL_OPTION:
                    correctGuesses = 0;
                    break;
                default:
                    System.out.println("Should never happen, but default case should always be included");
            }
        }
        else if(nextCard < cardGenerated)
        {
            switch(input)
            {
                case JOptionPane.NO_OPTION:
                    correctGuesses++;
                    break;
                case JOptionPane.YES_OPTION:
                case JOptionPane.CANCEL_OPTION:
                    correctGuesses = 0;
                    break;
                default:
                    System.out.println("Should never happen, but default case should always be included");
            }
        }
        else
        {
            switch(input)
            {
                case JOptionPane.CANCEL_OPTION:
                    correctGuesses++;
                    break;
                case JOptionPane.YES_OPTION:
                case JOptionPane.NO_OPTION:
                    correctGuesses = 0;
                    break;
                default:
                    System.out.println("Should never happen, but default case should always be included");
            }
        }