创建扑克牌时List add方法和预想的不一样

来源:7-1 简易扑克牌游戏

plorzzz

2016-12-13 09:40

public void pokerSetUp(){

Poker pokerCard = new Poker();

List pokerList = new ArrayList();

String[] pokerstr1 = {"黑桃","红桃","梅花","方片"};

String[] pokerstr2 = {"2","3","4","5","6","7","8","9","10","J","Q","k"};

System.out.println("----------创建扑克牌----------");

for(int i=0;i < 4;i++){

for(int j=0;j < 12;j++){

pokerCard.pokercard1 = pokerstr1[i];

// System.out.println(pokerstr1[i]);

// System.out.println(pokerCard.pokercard1);

pokerCard.pokercard2 = pokerstr2[j];

// System.out.println(pokerstr2[j]);

// System.out.println(pokerCard.pokercard2);

pokerList.add(pokerCard);

}

}

System.out.println("----------扑克牌创建成功----------");

System.out.println("为:");

/**

* 通过For each访问List

*/

for (Object obj : pokerList) {

Poker cr = (Poker) obj;

System.out.print(cr.pokercard1 + cr.pokercard2 + " ");

}

}


写回答 关注

2回答

  • TiGer196
    2017-02-04 21:51:51

    存错了,取错了

  • plorzzz
    2016-12-13 09:44:58


    // System.out.println(pokerstr1[i]);

    // System.out.println(pokerCard.pokercard1);

    测试发现在输入List之前我的每个元素都是正确的 但是使用add在循环中添加后用For each遍历输出出来全是 方片k

    头疼 56个方片k ....


Java入门第三季

Java中你必须懂得常用技能,不容错过的精彩,快来加入吧

409792 学习 · 4340 问题

查看课程

相似问题