【发布时间】:2014-11-02 20:59:50
【问题描述】:
我目前正在编写一个涉及随机卡片选择的程序,我想知道是否有办法让程序分别用 jack、queen 和 king 替换数字 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);
【问题讨论】:
-
写一个方法。这就是避免重复编写 4 次相同代码的方法。
-
使用
if语句,或者switch语句。