水务 网站建设,拍摄宣传片的流程简要,秦皇岛海三建设工程集团,柳州小程序制作公司目录 前言
问题
代码展现及分析 poker类
game类 Text类 前言
洗牌算法为ArrayList具体使用的典例#xff0c;可以很好的让我们快速熟系ArrayList的用法。如果你对ArrayList还不太了解除#xff0c;推荐先看本博主的ArrayList的详解。
ArrayList的详解_WHabcwu的博客-CSD…目录 前言
问题
代码展现及分析 poker类
game类 Text类 前言
洗牌算法为ArrayList具体使用的典例可以很好的让我们快速熟系ArrayList的用法。如果你对ArrayList还不太了解除推荐先看本博主的ArrayList的详解。
ArrayList的详解_WHabcwu的博客-CSDN博客 问题
我们需要一副完整的扑克牌除去大小王一共52张牌参与游戏的玩家共3名在洗牌后分发每名玩家5张扑克牌。 代码展现及分析
面向对象思想解决问题主要分为这3步 找出其中的所有的对象实现所有的对象完成对象之间的交互 我们可以想到这几个类扑克牌游戏的操作类 测试类。 poker类
描述扑克牌的属性为花色和数字我们对其进行封装再重写 toString方法
public class poker {private String suit;private String rank;public poker(String suit, String rank) {this.suit suit;this.rank rank;}public String getSuit() {return suit;}public void setSuit(String suit) {this.suit suit;}public String getRank() {return rank;}public void setRank(String rank) {this.rank rank;}Overridepublic String toString() {return { suit rank };}
}
game类
import java.util.ArrayList;
import java.util.List;
import java.util.Random;public class game {private static final String[] SUITS{♥,♠,♣,♦};public Listpoker buypoker(){Listpoker pokersnew ArrayList();for (int i 0; i SUITS.length; i) {for (int j 1; j 13 ; j) {poker poker new poker(SUITS[i], j);pokers.add(poker);}}return pokers;}public Listpoker suaffle(Listpoker pokers){Random random new Random();for (int i pokers.size(); i 0; i--) {int indxrandom.nextInt(i);poker tmppokers.get(i);pokers.set(i,pokers.get(indx));pokers.set(indx,tmp);}return pokers;}public void grant(Listpoker pokers){Listpoker hand1new ArrayList();Listpoker hand2new ArrayList();Listpoker hand3new ArrayList();ListListpoker handnew ArrayList();hand.add(hand1);hand.add(hand2);hand.add(hand3);for (int i 0; i 5; i) {for(int j0;j3;j){poker cardpokers.remove(0);hand.get(j).add(card);}}System.out.println(第一个人的牌);System.out.println(hand1);System.out.println(---------------------------);System.out.println(第二个人的牌);System.out.println(hand2);System.out.println(---------------------------);System.out.println(第三个人的牌);System.out.println(hand3);System.out.println(---------------------------);System.out.println(剩下的牌);System.out.println(pokers);}
}
buypoker方法的解析 suaffle方法的解析 grant方法的解析 Text类
public class Text {public static void main(String[] args) {game game new game();Listpoker pokersgame.buypoker();System.out.println(牌到手了);System.out.println(pokers);System.out.println(---------------------------);System.out.println(洗牌);pokersgame.suaffle(pokers);System.out.println(pokers);System.out.println(---------------------------);game.grant(pokers);}
} 以上为我个人的小分享如有问题欢迎讨论
都看到这了不如关注一下给个免费的赞