Java 用LinkdeList實現52張撲克牌

來源:互聯網
上載者:User

標籤:索引   print   lib   i++   length   lin   method   產生   epo   

用LinkdeList實現52張撲克牌(不含大小王)的洗牌功能。提示:花色 ,和數字分別用數組儲存。

 

 

import java.util.LinkedList;import java.util.Random;class Poker{        String color;//花色    String  num;//數字        public Poker(String color,String num){                this.color = color;        this.num = num;    }        @Override    public String toString() {        // TODO Auto-generated method stub        return "{"+color+num+"}";    }}public class HomeWork3 {    /**     * @param args     * 用LinkdeList實現52張撲克牌(不含大小王)的洗牌功能。     * 提示:花色 ,和數字分別用數組儲存。     */    public static void main(String[] args) {        // TODO Auto-generated method stub       // 1.建立撲克牌        LinkedList list = creatPoker();        //2.洗牌        shufflePoker(list);        //3.顯示        showPoker(list);                //4.發牌的功能。            }        //洗牌的功能    public static void shufflePoker(LinkedList poker){                //1.產生一個隨機數        Random rangdom =  new  Random();                for(int i = 0 ;i<100;i++){                        //隨機擷取兩個索引值:            int index1 = rangdom.nextInt(poker.size());            int index2 = rangdom.nextInt(poker.size());            Poker p1 = (Poker) poker.get(index1);            Poker p2 = (Poker) poker.get(index2);            //交換用set方法            poker.set(index1, p2);            poker.set(index2, p1);                    }            }        public static void showPoker(LinkedList list){                for(int i = 0;i<list.size();i++){                        System.out.print(list.get(i));            if( i % 13 == 12){                                System.out.println();            }                    }            }        public static LinkedList creatPoker(){        //建立集合對象用來存放撲克牌        LinkedList list = new LinkedList();        //定義花色和數字        String[] colors = {"黑桃","紅桃","方塊","梅花"};        String[] nums = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};                for(int i = 0 ;i<nums.length ;i++){                        for(int j = 0;j<colors.length;j++){                                list.add(new Poker(colors[j], nums[i]));            }        }                return list;    }}

 

Java 用LinkdeList實現52張撲克牌

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.