慕粉4078529
2016-12-14 23:53
public class PaiTest {
private List<Card> cardList;
public PaiTest(){
this.cardList = new ArrayList<Card>();
}
public void purcard(){
String[] sh= {"方片","黑桃","红桃","梅花"};
for(int i=0;i<4;i++){
Card cars = new Card();
cars.setShape(sh[i]);
for( int j=2;j<13;j++){
System.out.println("j的值:"+j);
cars.setId(j+"");
System.out.println("添加了卡牌:"+cars.getShape()+cars.getId());
cardList.add(cars);
}
}
for(Card card:cardList)
System.out.println("输出卡牌:"+card.getShape()+" "+card.getId());
}
public static void main(String[] args) {
PaiTest pt = new PaiTest();
pt.purcard();
}
}
输出卡牌:方片 12
输出卡牌:方片 12
输出卡牌:方片 12
输出卡牌:方片 12
输出卡牌:方片 12
输出卡牌:方片 12
输出卡牌:方片 12
输出卡牌:方片 12
输出卡牌:方片 12
输出卡牌:方片 12
输出卡牌:方片 12
输出卡牌:黑桃 12
输出卡牌:黑桃 12
输出卡牌:黑桃 12
输出卡牌:黑桃 12
输出卡牌:黑桃 12
输出卡牌:黑桃 12
输出卡牌:黑桃 12
输出卡牌:黑桃 12
输出卡牌:黑桃 12
sorry,原来问题在这, Card cars = new Card();创建对象要放到循环外面,不然每次循环都是创建一个新对象,那么在最后遍历的时候也就只有最后添加的“”12“”了,就这样!
就这样!
public void purcard(){
String[] sh= {"方片","黑桃","红桃","梅花"};
for(int i=0;i<4;i++){
Card cars = new Card();
cars.setShape(sh[i]);
for( int j=2;j<13;j++){
System.out.println("j的值:"+j);
cars.setId(j+"");
System.out.println("添加了卡牌:"+cars.getShape()+cars.getId());
cardList.add(cars);
}
}
for(Card card:cardList)
System.out.println("输出卡牌:"+card.getShape()+" "+card.getId());
}
在这个purcard()方法中的两个for循环,你的大括号位置不对,我猜你是拷贝程序的时候没有注意粘贴位置,
Java入门第三季
409787 学习 · 4340 问题
相似问题