最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java使用ArrayList實(shí)現(xiàn)撲克牌的示例代碼

 更新時間:2022年10月04日 09:27:25   作者:摸魚王胖嘟嘟  
學(xué)習(xí)了關(guān)于集合類的知識,我們可以做一個小項(xiàng)目來加深對集合類知識的學(xué)習(xí)!本文就來利用ArrayList實(shí)現(xiàn)撲克牌發(fā)牌洗牌效果,需要的可以參考一下

前言

學(xué)習(xí)了關(guān)于集合類的知識,我們可以做一個小項(xiàng)目來加深對集合類知識的學(xué)習(xí)!

一、項(xiàng)目要求

代碼實(shí)現(xiàn),一副撲克牌(不包括大小王)的購買、打亂、發(fā)牌。

二、具體實(shí)現(xiàn)

2.1 Card類

class Card {
    private int rank;//數(shù)字
    private String suit;//花色

    public Card(int rank, String suit) {
        this.rank = rank;
        this.suit = suit;
    }

    @Override
    public String toString() {
        return "[ " + this.suit + ":"+this.rank+" ]";
    }
}

2.2 生成撲克牌

 private static final String[] suits = {"?", "?", "?", "?"};

    //假設(shè)沒有大小王:1 2 3............. 11 12 13
    public static List<Card> buyCard() {
        ArrayList<Card> cards = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            for (int j = 1; j <= 13; j++) {
//                String suit = suits[i];
//                int rank = j;
//                Card card = new Card(rank, suit);
//                cards.add(card);
                cards.add(new Card(j,suits[i]));
            }
        }
        return cards;
    }

2.3 打亂順序

 private static void swap(List<Card> cards, int i, int j) {
        Card tmp = cards.get(i);
        cards.set(i,cards.get(j));
        cards.set(j,tmp);
    }

    //洗牌
    public static void shuffle(List<Card> cards) {
        int size = cards.size();
        for (int i = size-1; i > 0 ; i--) {
            Random random = new Random();
            int rand = random.nextInt(i);
            swap(cards, i, rand);
        }
    }

2.4 發(fā)牌

		System.out.println("揭牌:3個人每個人輪流揭牌5張牌");

        ArrayList<List<Card>> hand = new ArrayList<>();

        List<Card> hand1 = new ArrayList<>();
        List<Card> hand2 = new ArrayList<>();
        List<Card> hand3 = new ArrayList<>();
        
        hand.add(hand1);
        hand.add(hand2);
        hand.add(hand3);

        //每個人,輪流揭牌
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 3; j++) {
                Card card = cards.remove(0);
                hand.get(j).add(card);
            }
        }

三、Test.java

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

class Card {
    private int rank;//數(shù)字
    private String suit;//花色

    public Card(int rank, String suit) {
        this.rank = rank;
        this.suit = suit;
    }

    @Override
    public String toString() {
        return "[ " + this.suit + ":"+this.rank+" ]";
    }
}
public class Test1 {
    private static final String[] suits = {"?", "?", "?", "?"};

    //假設(shè)沒有大小王:1 2 3............. 11 12 13
    public static List<Card> buyCard() {
        ArrayList<Card> cards = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            for (int j = 1; j <= 13; j++) {
//                String suit = suits[i];
//                int rank = j;
//                Card card = new Card(rank, suit);
//                cards.add(card);
                cards.add(new Card(j,suits[i]));
            }
        }
        return cards;
    }

    private static void swap(List<Card> cards, int i, int j) {
        Card tmp = cards.get(i);
        cards.set(i,cards.get(j));
        cards.set(j,tmp);
    }

    //洗牌
    public static void shuffle(List<Card> cards) {
        int size = cards.size();
        for (int i = size-1; i > 0 ; i--) {
            Random random = new Random();
            int rand = random.nextInt(i);
            swap(cards, i, rand);
        }
    }

    public static void main(String[] args) {
        List<Card> cards = buyCard();
        System.out.println("買牌:" + cards);
        shuffle(cards);
        System.out.println("洗牌:" + cards);
        System.out.println("揭牌:3個人每個人輪流揭牌5張牌");

        ArrayList<List<Card>> hand = new ArrayList<>();

        List<Card> hand1 = new ArrayList<>();
        List<Card> hand2 = new ArrayList<>();
        List<Card> hand3 = new ArrayList<>();
        
        hand.add(hand1);
        hand.add(hand2);
        hand.add(hand3);

        //每個人,輪流揭牌
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 3; j++) {
                Card card = cards.remove(0);
                hand.get(j).add(card);
            }
        }
        System.out.println("第一個人的牌:"+ hand1);
        System.out.println("第二個人的牌:"+ hand2);
        System.out.println("第三個人的牌:"+ hand3);
        System.out.println("剩下的牌:"+cards);
    }

    public static void main1(String[] args) {
        // 1. 構(gòu)造一副撲克牌
        // 2. 揭牌
        Card card = new Card(3,"?");
        System.out.println(card);

    }
}

到此這篇關(guān)于Java使用ArrayList實(shí)現(xiàn)撲克牌的示例代碼的文章就介紹到這了,更多相關(guān)Java ArrayList撲克牌內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java中緩沖流的使用與性能提升(讓文件操作更高效)

    Java中緩沖流的使用與性能提升(讓文件操作更高效)

    本文通過實(shí)例代碼介紹了Java中緩沖流的概念、工作原理和性能提升,并提供了字節(jié)緩沖流和字符緩沖流的使用示例,緩沖流通過在內(nèi)存中創(chuàng)建緩沖區(qū),減少實(shí)際的I/O操作次數(shù),從而提升文件讀寫性能,結(jié)合實(shí)例代碼給大家講解的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧
    2024-12-12
  • 簡單分析Java的求值策略原理

    簡單分析Java的求值策略原理

    在本篇文章里小編給大家整理的是一篇關(guān)于簡單分析Java的求值策略原理內(nèi)容,有需要的朋友們可以學(xué)習(xí)下。
    2021-06-06
  • springboot 動態(tài)數(shù)據(jù)源的實(shí)現(xiàn)方法(Mybatis+Druid)

    springboot 動態(tài)數(shù)據(jù)源的實(shí)現(xiàn)方法(Mybatis+Druid)

    這篇文章主要介紹了springboot 動態(tài)數(shù)據(jù)源的實(shí)現(xiàn)方法(Mybatis+Druid),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-01-01
  • Springboot靜態(tài)資源訪問實(shí)現(xiàn)代碼解析

    Springboot靜態(tài)資源訪問實(shí)現(xiàn)代碼解析

    這篇文章主要介紹了Springboot靜態(tài)資源訪問實(shí)現(xiàn)代碼解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-06-06
  • Maven+oracle+SSM搭建簡單項(xiàng)目的方法

    Maven+oracle+SSM搭建簡單項(xiàng)目的方法

    本篇文章主要介紹了Maven+oracle+SSM搭建簡單項(xiàng)目的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-03-03
  • Java線程池ThreadPoolExecutor源碼深入分析

    Java線程池ThreadPoolExecutor源碼深入分析

    ThreadPoolExecutor作為java.util.concurrent包對外提供基礎(chǔ)實(shí)現(xiàn),以內(nèi)部線程池的形式對外提供管理任務(wù)執(zhí)行,線程調(diào)度,線程池管理等等服務(wù)
    2022-08-08
  • Java中使用ZXing和QRCode生成二維碼的示例詳解

    Java中使用ZXing和QRCode生成二維碼的示例詳解

    生成二維碼在Java中有多種方法,常用的是通過第三方庫來實(shí)現(xiàn),比較流行的庫包括?ZXing?(Zebra?Crossing)?和?QRCode,本文小編就給大家介紹了Java中使用ZXing和QRCode生成二維碼的示例,需要的朋友可以參考下
    2024-09-09
  • Jenkins自動構(gòu)建部署項(xiàng)目到遠(yuǎn)程服務(wù)器上的方法步驟

    Jenkins自動構(gòu)建部署項(xiàng)目到遠(yuǎn)程服務(wù)器上的方法步驟

    這篇文章主要介紹了Jenkins自動構(gòu)建部署項(xiàng)目到遠(yuǎn)程服務(wù)器上的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • 啟動Tomcat時出現(xiàn)大量亂碼的解決方法

    啟動Tomcat時出現(xiàn)大量亂碼的解決方法

    今天給大家?guī)淼氖顷P(guān)于Java的相關(guān)知識,文章圍繞著啟動Tomcat時出現(xiàn)大量亂碼的解決方法展開,文中有非常詳細(xì)的介紹及圖文示例,需要的朋友可以參考下
    2021-06-06
  • 關(guān)于Java反射給泛型集合賦值問題

    關(guān)于Java反射給泛型集合賦值問題

    這篇文章主要介紹了Java反射給泛型集合賦值,需要的朋友可以參考下
    2022-01-01

最新評論

东乡族自治县| 星座| 江油市| 维西| 兰西县| 武宣县| 斗六市| 临海市| 新龙县| 临洮县| 伊金霍洛旗| 册亨县| 离岛区| 共和县| 云阳县| 宁都县| 体育| 白朗县| 昌宁县| 全州县| 南汇区| 冀州市| 普安县| 虎林市| 长顺县| 赣州市| 景洪市| 阿拉善右旗| 长海县| 宝兴县| 徐汇区| 望城县| 香格里拉县| 永定县| 怀化市| 梁河县| 裕民县| 荃湾区| 青铜峡市| 海城市| 松江区|