-1

嗨,我写了一些代码来打印一副卡片

这是怎么出来的:2C 2D 2H 2S 3C 3D 3H 3S 4C ...

这就是我想要的:2C 3C 4C 5C 6C ...

这是我的代码的一部分:

public DeckOfCards()
{
  for (PlayingCard.CardRank rank : PlayingCard.CardRank.values())   
    for (PlayingCard.CardSuit suit : PlayingCard.CardSuit.values())           
    {
       System.out.printf(" %c%c ", rank.getSymbol(),suit.getSymbol());
    }
}

我打算使用 for 循环,但我不确定从哪里开始任何想法?

4

3 回答 3

3

您非常接近:切换两个循环以获得您正在寻找的效果。

for (PlayingCard.CardSuit suit : PlayingCard.CardSuit.values())
    for (PlayingCard.CardRank rank : PlayingCard.CardRank.values())

其他一切看起来都不错。

于 2012-05-17T02:28:16.610 回答
0

交换嵌套 for 循环的顺序。也就是说,将其更改为:

for (PlayingCard.CardSuit suit : PlayingCard.CardSuit.values())
      for (PlayingCard.CardRank rank : PlayingCard.CardRank.values())   

请记住,每次通过外循环时,内循环都会运行一次。:)

于 2012-05-17T02:29:19.663 回答
0

在我看来,它可能很简单:

for (PlayingCard.CardSuit suit : PlayingCard.CardSuit.values())
{  
    for (PlayingCard.CardRank rank : PlayingCard.CardRank.values())   

    {
       System.out.printf(" %c%c ", rank.getSymbol(),suit.getSymbol());
    }
}

.. 换掉两个嵌套的 for 循环。

于 2012-05-17T02:30:30.033 回答