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) { Collectioncoll = new ArrayList (); 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) { ArrayListpokerBox = new ArrayList (); ArrayList colors = new ArrayList (); ArrayList numbers = new ArrayList (); 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 player1 = new ArrayList (); ArrayList player2 = new ArrayList (); ArrayList player3 = new ArrayList (); ArrayList dipai = new ArrayList (); for(int i = 0;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); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)