遍历数组
public class NBF {
public static void main(String[] args) {
int[] arr = {344,55,76,87};
//使用增强for遍历数组
for(int a : arr){//a代表数组中的每个元素
System.out.println(a);
}
}
}
遍历集合
public class NBF {
public static void main(String[] args) {
Collection<String> coll = new ArrayList<String>();
coll.add("1");
coll.add("2");
coll.add("3");
//使用增强for遍历
for(String s :coll){//接收变量s代表 代表被遍历到的集合元素
System.out.println(s);
}
}
}
斗地主实现
public class Joker {
public static void main(String[] args) {
ArrayList<String> pokerBox = new ArrayList<String>();
ArrayList<String> colors = new ArrayList<String>();
ArrayList<String> numbers = new ArrayList<String>();
colors.add("♥");
colors.add("♦");
colors.add("♠");
colors.add("♣");
for(int i = 2;i<=10;i++){
numbers.add(i+"");
}
numbers.add("J");
numbers.add("Q");
numbers.add("K");
numbers.add("A");
for (String color : colors) {
for(String number : numbers){
String card = color+number;
pokerBox.add(card);
}
}
pokerBox.add("小☺");
pokerBox.add("大☠");
Collections.shuffle(pokerBox);
ArrayList<String> player1 = new ArrayList<String>();
ArrayList<String> player2 = new ArrayList<String>();
ArrayList<String> player3 = new ArrayList<String>();
ArrayList<String> dipai = new ArrayList<String>();
for(int i = 0;i<pokerBox.size();i++){
String card = pokerBox.get(i);
if(i>=51){
dipai.add(card);
} else {
if(i%3==0){
player1.add(card);
}else if(i%3==1){
player2.add(card);
}else{
player3.add(card);
}
}
}
System.out.println("玩家1:"+player1);
System.out.println("玩家2:"+player2);
System.out.println("玩家3:"+player3);
System.out.println("底牌:"+dipai);
}
}