猿问

嵌套for循环以打印数组列表中的一组值

我正在创建一个程序来显示扑克牌的数组列表。到目前为止,我有两个枚举集,分别是套装和等级。我可以这样显示它们

S2 S3 S4 S5 S6 S7 S8 S9 S10 SJ SQ SK SA H2 H3 H4 H5 H6 H7 H8 H9 H10 HJ HQ HK HA D2 D3 D4 D5 D6 D7 D8 D9 D10 DJ DQ DK DA C2 C3 C4 C5 C6 C7 C8 C9 C10 CJ CQ CK CA

但是,有没有一种方法可以使用嵌套的 for 循环来拆分西装并分别显示它们,如下所示?

S2 S3 S4 S5 S6 S7 S8 S9 S10 SJ SQ SK SA H2 H3 H4 H5 H6 H7 H8 H9 H10 HJ HQ HK HA D2 D3 D4 D5 D6 D7 D8 D9 D10 DJ DQ DK DA C2 C3 C4 C5 C6 C7 C8 C9 C10 CJ CQ CK CA

这是我到目前为止所尝试的:

显示甲板方法

private static final int MAXC = 13;

private static final int MAXD = 52;


private static void displayDeck(ArrayList<Decks> values)



{

    for (int i = 0; i < MAXD; i++)

    {

    // this prints the entire deck out  

    System.out.printf ("%s", values.get(i));


    }

}

我尝试了这个嵌套的 for 循环但无济于事


{

    for (int i = 0; i < MAXD; i++)

    {

        for(int j =0; j < MAXC; j++)

        {

    System.out.printf ("%s", values.get(j));

        }

    }

}

添加卡组方法


    // enhanced for loop to add every single card into the arrayList

private void addDeck(ArrayList<Decks> values)

{

    for(SuitEnum s : SuitEnum.values ())

    {

        for(RankEnum r : RankEnum.values ())

        {

            values.add(new PlayingCard(s,r));

        }

    }


}

主要方法


 public static void main(String args[]) 

 {

      ArrayList<Decks> list = new ArrayList<Decks>();  


      decks test = new decks ();

      test.addDeck(list);

      printDeck(list);

 }


泛舟湖上清波郎朗
浏览 117回答 3
3回答

MYYA

尝试这个:{&nbsp; &nbsp; for (int i = 0; i < MAXD; i++)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; for(int j =0; j < MAXC; j++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.printf ("%s", values.get(i).get(j));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}

动漫人物

尝试这个,private void addDeck(ArrayList<Decks> values){&nbsp; &nbsp; int suiteLength = SuitEnum.values().length;&nbsp; &nbsp; int rankLength = RankEnum.values().length;&nbsp; &nbsp; for(int i=0; i<suiteLength; i++)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; for(int j=0; j<rankLength; j++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int index = rankLength * i + j;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.printf ("%s", values.get(index));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}

qq_笑_17

尝试这个:{&nbsp; &nbsp; for (int i = 0; i < MAXD; i++)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; for(int j =0; j < MAXC; j++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int index = i + j * MAXD;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.printf ("%s", values.get(index));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
随时随地看视频慕课网APP

相关分类

Java
我要回答