【发布时间】:2014-11-15 18:32:08
【问题描述】:
看下面的程序:
public class HouseOfCards
{
public static void main(String[] args)
{
for (int cards = 1; cards <= 4; cards++)
{
if (cards == 1)
{
System.out.println("Ace of Clubs");
for (int singles = 2; singles <= 9; singles++)
{
System.out.println(singles + " of Clubs");
}//end of for loop()
System.out.println("Jack of Clubs");
System.out.println("Queen of Clubs");
System.out.println("King of Clubs");
System.out.println("Ace of Clubs");
}//end of if()
......
//More else if() blocks for each suit
......
}//end of for loop()
}//end of method main()
}//end of class HouseOfCards
在上面的代码中,我想打印第一组卡片,即梅花,然后以“新套牌顺序”格式对其余的花色执行相同的操作。
梅花 --> 黑桃 --> 红心 --> 方块
我看到第一个 if() 块,即 (cards == 1),有点重复。我不想做 4 个 if 块来做整个套牌。
我的问题如下, 1.我将如何以这种方式减少代码? 2. 可能吗? 要么 3. 是否最好为每套花色做 4 组 if() 块?
提前感谢您的帮助!
【问题讨论】: