遇到的问题:
1.int和Integer的区别?
1)Integer是int的包装类,int则是java的一种基本数据类型 。
2)Integer变量必须实例化后才能使用,而int变量不需要 。
3)Integer实际是对象的引用,当new一个Integer时,实际上是生成一个指针指向此对象;而int则是直接存储数据值 。
4)Integer的默认值是null,int的默认值是0。
Cards.java:
import java.util.*;
public class Card{
public static void main(String[] args){
ArrayList<String> color = new ArrayList<String>();
ArrayList<String> num = new ArrayList<String>();
color.add("♠️");
color.add("♥️");
color.add("◇");
color.add("♣️");
for(int i=2; i<=10; i++){
//很神奇,可以把add()里面的数据变成字符串
num.add(i+ "");
}
num.add("J");
num.add("A");
num.add("K");
num.add("Q");//map是一副牌 54张
HashMap<Integer, String> map = new HashMap<Integer, String>();
int index = 0;
for (String thisColor: color){
for (String thisNum: num){
map.put(index++, thisColor + thisNum);
}
}
map.put(index++, "小王");
map.put(index++, "大王");
ArrayList<Integer> cards = new ArrayList();
//这里就是int
for(int i=0; i<54; i++){
cards.add(i);
}
//将cards顺序打乱
Collections.shuffle(cards);
//System.out.println(cards);
//System.out.println(map);
//创建三个玩家
ArrayList<Integer> player1 = new ArrayList();
ArrayList<Integer> player2 = new ArrayList();
ArrayList<Integer> player3 = new ArrayList();
ArrayList<Integer> SecreatCards = new ArrayList();
for(int i=0; i<cards.size(); i++){
if(i>50){
SecreatCards.add(cards.get(i));
}else if(i%3 == 0){
player1.add(cards.get(i));
}else if(i%3 == 1){
player2.add(cards.get(i));
}else {
player3.add(cards.get(i));
}
}
Collections.sort(player1);
Collections.sort(player2);
Collections.sort(player3);
ArrayList<String> sPlayer1 = new ArrayList<String>();
ArrayList<String> sPlayer2 = new ArrayList<String>();
ArrayList<String> sPlayer3 = new ArrayList<String>();
for(Integer key : player1){
//这里的key应该是object类,所以用Integer而不是int
sPlayer1.add(map.get(key));
}
for(Integer key : player2){
sPlayer2.add(map.get(key));
}
for(Integer key : player3){
sPlayer3.add(map.get(key));
}
System.out.println("player1:" + sPlayer1);
System.out.println("player2:" + sPlayer2);
System.out.println("player3:" + sPlayer3);
System.out.println("底牌:" + SecreatCards);
}
}
还是蛮有意思的~