- import java.util.*;
- class Test02
- {
- public static void main(String[] args)
- {
-
- newPk();
- }
- /*思考:
- 使用打乱算法洗牌,发牌
- 提示:需要两个数组
- 数组1:定义一副牌
- 数组2:确定玩家,斗地主{"沈春阳","张柏芝","林青霞"}
- * 洗牌
- * 发牌:0,1,2 牌%3
- */
- public static void newPk()
- {
- //定义一副牌,用数组
- String[] pk = {"红桃A","黑桃A","方块A","梅花A","红桃2","黑桃2","方块2","梅花2",
- "红桃3","黑桃3","方块3","梅花3","红桃4","黑桃4","方块4","梅花4",
- "红桃5","黑桃5","方块5","梅花5","红桃6","黑桃6","方块6","梅花6",
- "红桃7","黑桃7","方块7","梅花7","红桃8","黑桃8","方块8","梅花8",
- "红桃9","黑桃9","方块9","梅花9","红桃10","黑桃10","方块10","梅花10",
- "红桃J","黑桃J","方块J","梅花J","红桃Q","黑桃Q","方块Q","梅花Q",
- "红桃K","黑桃K","方块K","梅花K","jorker","JORKER"};
- //首先将牌随机洗乱,然后用一数组储存
- String[] arr = new String[54];
- boolean[] base = new boolean[54];
- int a = 0;
- do
- {
- Random r = new Random();
- int temp = r.nextInt(arr.length);
- if(!base[temp])
- {
- arr[a++] = pk[temp];
- base[temp] = true;
- }
- }
- while (a<pk.length);
- //需要将洗好的牌按顺序发给3个人,我们用三个数组来储存
- String[] name1 = new String[17];
- String[] name2 = new String[17];
- String[] name3 = new String[17];
- int i=0,j=0,k=0;
- for (int x = 0; x<51;x++ )
- {
- if(x%3==0)
- {
- name1[i++] = arr[x];
- }
- if(x%3==1)
- {
- name2[j++] = arr[x];
- }
- if(x%3==2)
- {
- name3[k++] = arr[x];
- }
- }
- System.out.println(Arrays.toString(arr));//洗后的牌
- System.out.println("--------------------------------------------------------");
- System.out.println(Arrays.toString(name1));//第一个人的牌
- System.out.println("--------------------------------------------------------");
- System.out.println(Arrays.toString(name2));//第二个人的牌
- System.out.println("--------------------------------------------------------");
- System.out.println(Arrays.toString(name3));//第三个人的牌
- //还需要定义一个数组来装最后三张牌,也是按顺序
- System.out.println("--------------------------------------------------------");
- String[] lastPk = new String[3];
- lastPk[0] = arr[51];
- lastPk[1] = arr[52];
- lastPk[2] = arr[53];
- System.out.println(Arrays.toString(lastPk));
- }
- }
复制代码
上面楼层的,我也是刚学到数组,但是我好像定义的数组太多了 |