【Java】随机发牌程序
生活随笔
收集整理的這篇文章主要介紹了
【Java】随机发牌程序
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
import java.util.Random;public class Cards {/*** 初始化標志數組,牌均未發出*/private int[][] cards = new int[4][13];/*** 花色*/private String[] suits;/*** 點數*/private String[] points;/*** 構造器初始化花色、點數* @param suits* @param points*/public Cards(String[] suits, String[] points) {super();this.suits = suits;this.points = points;}public int[][] getCards() {return cards;}public void setCards(int[][] cards) {this.cards = cards;}public String[] getSuits() {return suits;}public void setSuits(String[] suits) {this.suits = suits;}public String[] getPoints() {return points;}public void setPoints(String[] points) {this.points = points;}public void sendCards(int n) {//記錄撲克牌信息String sendCards = new String("發放的撲克牌為:");//生成隨機數的生成對象Random randomBuilder = new Random();//定義發放撲克牌花色、點數int suit, point;for (int i = 0; i < n; ) {suit = randomBuilder.nextInt(4);point = randomBuilder.nextInt(13);if (cards[suit][point] == 1) {continue;} else {cards[suit][point] = 1;//追加新發撲克牌花色sendCards = sendCards + suits[suit];//追加點數sendCards = sendCards + points[point] + " ";//準備發放下一張牌i++;}}System.out.println(sendCards.toString());}public static void main(String[] args) {String[] suits = {"紅心", "方片", "梅花", "黑桃"};String[] points = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};Cards cards = new Cards(suits, points);cards.sendCards(10);}}
運行程序,發現在0<=x<=52范圍內可以得到隨機發牌的準確結果。
提供代碼的main方法里是發10張牌,運行示例:
總結
以上是生活随笔為你收集整理的【Java】随机发牌程序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Java】对JTable里的元素进行排
- 下一篇: 用Java简便地去重+排序(洛谷P105