gpt4 book ai didi

java - 为号码指定名称

转载 作者:行者123 更新时间:2023-12-01 22:37:23 24 4
gpt4 key购买 nike

我目前正在编写一个涉及随机选牌的程序,我想知道是否有办法让程序分别用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com