服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服务器之家 - 编程语言 - JAVA教程 - Java实现洗牌发牌的方法

Java实现洗牌发牌的方法

2019-12-29 14:21罪恶的花生 JAVA教程

这篇文章主要介绍了Java实现洗牌发牌的方法,涉及java针对数组的遍历与排序操作相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了Java实现洗牌发牌的方法。分享给大家供大家参考。具体如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import java.util.*;
public class Main {
  /**
   * @param args the command line arguments
   */
  static int numbersOfPlayers = 4;
  static int numbersOfHandCard = 13;
  public static void main(String[] args) {
    // TODO code application logic here
    String[] player = {"小王","小张","小赵","小白"};
    String sentence = "的手牌是:";
    String[] huase = {"♥","♣","♦","♠"};
    String[] paiValue ={"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
    List deak = new ArrayList();
    for(int i = 0;i < 4;i++){
      for(int j = 0; j < 13;j++)
      {
        deak.add(huase[i]+paiValue[j]);
      }
    }
    Collections.shuffle(deak);
    for(int i = 0;i < numbersOfPlayers;i++){
      System.out.print(player[i]+sentence);
      System.out.println(dealHand(deak, numbersOfHandCard));
    }
  }
  public static List dealHand(List Ls,int n){
    int sizePai = Ls.size();
    List handView = Ls.subList(sizePai - n, sizePai);
    List hand = new ArrayList(handView);
    handView.clear();
    return hand;
  }
}

希望本文所述对大家的java程序设计有所帮助。

延伸 · 阅读

精彩推荐