为了账号安全,请及时绑定邮箱和手机立即绑定

在 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();

}



正在回答

2 回答

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

0 回复 有任何疑惑可以回复我~

你这种定义方式很麻烦.对于扑克牌这个对象,其实最简单的就是分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("创建一副扑克牌,成功!");


0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

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

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信