问答详情
源自:7-1 简易扑克牌游戏

在 realpokeToSelect.add(temp);这句代码中,为何我输出的temp.number一直都是A?

package SPG;


import java.util.ArrayList;

import java.util.Arrays;

import java.util.List;


public class SimplePokeGame {


public void testcreatpoke(){

List<Poke> temppokeToSelect= new ArrayList<Poke>();

List<Poke> realpokeToSelect= new ArrayList<Poke>();

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

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

   Poke[] poke={new Poke("黑桃",""),new Poke("红桃",""),new Poke("梅花",""),new Poke("方块","")};

   temppokeToSelect.addAll(Arrays.asList(poke));

   Poke temp=new Poke();

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

    temp= temppokeToSelect.get(i);

for(int k=0;k<13;k++){

  temp.number=x[k];

  System.out.println(temp.number);

  System.out.println("扑克牌为:"+temp.color+temp.number);

      realpokeToSelect.add(temp);

}

}

   Poke a= realpokeToSelect.get(0);

   Poke b= realpokeToSelect.get(1);

   Poke c= realpokeToSelect.get(2);

   System.out.println(a.number+":"+b.number+":"+c.number);

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

   for (Poke poke2 : realpokeToSelect) {

System.out.println("扑克牌为:"+poke2.color+poke2.number);

}

//    System.out.println();

   }

public static void main(String[] args) {

        SimplePokeGame it = new SimplePokeGame();

        it.testcreatpoke();

}



提问者:聚水弥香 2016-03-03 11:08

个回答

  • 无尽永恒
    2016-03-06 12:58:20
    已采纳

    不妨试试改realpokeToSelect.add(temp);为 realpokeToSelect.add(new Poke(temp.color,temp.number));

  • qq_侯杰_0
    2016-04-11 14:11:11

    你这种定义方式很麻烦.对于扑克牌这个对象,其实最简单的就是分ID,数值,花色这三个属性.ID负责用来判断牌面的大小,花色和数值就正常.创建起来就很简单.

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


    for (int i = 0; i < str.length; i++) {

    Poker pkbm=new Poker(i*4,str[i],"梅花");

    Poker pkrf=new Poker(i*4+1,str[i],"方片");

    Poker pkrh=new Poker(i*4+2,str[i],"红桃");

    Poker pkbh=new Poker(i*4+3,str[i],"黑桃");

       pkList.add(pkrh);

        pkList.add(pkrf);

        pkList.add(pkbh);

        pkList.add(pkbm);

    }

    System.out.println("创建一副扑克牌,成功!");