Java基础练习

Java基础练习_第1张图片
Java基础练习_第2张图片
下面是源码:

package com.whf.pukepai;

import java.util.LinkedList;
import java.util.Random;

/** 
 * @author :辰
 * E-mail: [email protected] 
 * 创建时间:2017-3-21 下午5:58:28 
 */
public class Demo_01 {

    public static void main(String[] args) {

        LinkedList pokers = createPoker();
        shufflePoker(pokers);
        showPoker(pokers);


    }

    //显示扑克牌
    private static void showPoker(LinkedList pokers) {
        // TODO Auto-generated method stub
        for (int i = 0; i < pokers.size(); i++) {
            System.out.print(pokers.get(i));
            //换行
            if (i%10 == 9) {
                System.out.println();
            }
        }
    }

    //洗牌功能
    private static void shufflePoker(LinkedList pokers) {
        //创建随机对象
        Random random = new Random();
        for (int i = 0; i <100; i++) {
            //随机产生两个索引值
            int index1 = random.nextInt(pokers.size());
            int index2 = random.nextInt(pokers.size());
            //根据索引值取出两张牌,然后交换两张牌的顺序
            Poker poker1 = (Poker) pokers.get(index1);
            Poker poker2 = (Poker) pokers.get(index2);
            pokers.set(index1, poker2);
            pokers.set(index2, poker1);
        }
    }


    //  生成扑克牌的方法
    private static LinkedList createPoker() {
        //该集合用于存储扑克对象
        LinkedList list = new LinkedList();
        //定义数组所在的花色与点数
        String[] colors = {"红色","黑色","方块","梅花",};
        String[] numers = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
        for (int j = 0; j < colors.length; j++) {
            for (int i = 0; i < numers.length; i++) {
                list.add(new Poker(colors[j],numers[i]));
            }
        }
        return list;
    }

}
package com.whf.pukepai;
/** 
 * @author :辰
 * E-mail: [email protected] 
 * 创建时间:2017-3-21 下午5:58:59 
 *  
 */
public class Poker {
    String color;// 花色
    String num;//   点数
    public Poker(String color, String num) {
        super();
        this.color = color;
        this.num = num;
    }
    @Override
    public String toString() {
        return "{" + color +  num + "}";
    }

}

你可能感兴趣的:(二维数组,java,每日一水,java基础)