日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

集合综合案例

發布時間:2025/4/16 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 集合综合案例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

案例介紹



斗地主綜合案例:
? ? ? ? 1.準備牌
? ? ? ? 2.洗牌
? ? ? ? 3.發牌
? ? ? ? 4.看牌

package com.itheima.demo04.Test;import java.util.ArrayList; import java.util.Collections;public class DouDiZhu {public static void main(String[] args) {//1.準備牌//定義一個存儲54張牌的ArrayList集合,泛型使用StringArrayList<String> poker = new ArrayList<>();//定義兩個數組,一個數組存儲牌的花色,一個數組存儲牌的序號String[] colors = {"?","?","?","?"};String[] numbers = {"2","A","K","Q","J","10","9","8","7","6","5","4","3"};//先把大王和小王存儲到poker集合中poker.add("大王");poker.add("小王");//循環嵌套遍歷兩個數組,組裝52張牌for(String number : numbers){for (String color : colors) {//System.out.println(color+number);//把組裝好的牌存儲到poker集合中poker.add(color+number);}}//System.out.println(poker);/*2.洗牌使用集合的工具類Collections中的方法static void shuffle(List<?> list) 使用默認隨機源對指定列表進行置換。*/Collections.shuffle(poker);//System.out.println(poker);/*3.發牌*///定義4個集合,存儲玩家的牌和底牌ArrayList<String> player01 = new ArrayList<>();ArrayList<String> player02 = new ArrayList<>();ArrayList<String> player03 = new ArrayList<>();ArrayList<String> diPai = new ArrayList<>();/*遍歷poker集合,獲取每一張牌使用poker集合的索引%3給3個玩家輪流發牌剩余3張牌給底牌注意:先判斷底牌(i>=51),否則牌就發沒了*/for (int i = 0; i < poker.size() ; i++) {//獲取每一張牌String p = poker.get(i);//輪流發牌if(i>=51){//給底牌發牌diPai.add(p);}else if(i%3==0){//給玩家1發牌player01.add(p);}else if(i%3==1){//給玩家2發牌player02.add(p);}else if(i%3==2){//給玩家3發牌player03.add(p);}}//4.看牌System.out.println("劉德華:"+player01);System.out.println("周潤發:"+player02);System.out.println("周星馳:"+player03);System.out.println("底牌:"+diPai);} }

?

總結

以上是生活随笔為你收集整理的集合综合案例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。