java - 为号码指定名称

标签 java random naming

我目前正在编写一个涉及随机选牌的程序,我想知道是否有办法让程序分别用 jack 、皇后和国王替换数字 11、12 和 13?我可以使用 if 语句来检测大于 10 的值,但这会迫使我编写相同的代码大约 4 次,这似乎适得其反。非常感谢所有回复!

int card0 = (cardGenerator.nextInt(13) + 2);
        int winTest = 4;
        while(winTest > 0)
        {
            Object[] highLowEqual = { "higher", "lower", "equal", "Quit" };
            Object userChoice = JOptionPane.showInputDialog(null,
            "The current card is " + card0 + ". Which do you think the next card will be? Remember: Ace is the highest possible.", "HiLo",
            JOptionPane.INFORMATION_MESSAGE, null,
            highLowEqual, highLowEqual[0]);
            int card1 = (cardGenerator.nextInt(13) + 2);

最佳答案

有一个知道如何打印自身的 Card 类:

public class Card
{
    int rank; 

    // ...

    public String toString()
    {
        switch( rank )
        {
            case 1: return "Ace";
            case 2:
            case 3:
            case 4:
            case 5:
            case 6:
            case 7:
            case 8:
            case 9:
            case 10: return "" + rank;
            case 11: return "Jack";
            case 12: return "Queen";
            case 13: return "King";
            default: return "INVALID CARD RANK";
        }
    }
}

关于java - 为号码指定名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26704334/

相关文章:

python - 用 Python 在文本文件中写入生成的数字

JavaScript:对传递给函数的变量使用相同的名称是好习惯吗?

java - 预购打印带缩进的二叉树

java - 如何将带时区的 PostgreSQL 时间戳转换为 Java Instant 或 OffSetDateTime?

python - 生成仅填充值 0 或 1 的随机稀疏矩阵

ruby - 为什么我要为 Array#shuffle 使用自定义 RNG?

java - 根据文本字段中的文本从数据库填充 jcombobox

java - 如何编写必要的 java 类以对以下 json 对象使用 gson.fromJson() ?

错误代码枚举的 C 命名建议

c - C 中的命名宏有限制/问题吗?