首先生成一副扑克牌(这里不包含大小王,自己练习的时候可以加上),然后根据用户输入的玩家人数和发给每名玩家的牌数进行发牌。
提示:可以使用shuffle()实现“洗牌”操作。
package test;
import java.util.*;
import static java.util.Collection.*;
public class test3 {
public static void main(String[] args) {
System.out.println("请输入玩家人数:");
int numHands = new Scanner(System.in).nextInt();
System.out.println("请输入每手牌的牌数:");
int num_card = new Scanner(System.in).nextInt();
List huase = Arrays.asList("♠","♥","♣","♦");
List numb = Arrays.asList("A","2","3","4","5","6","7","8","9","10","J","Q","K");
List deck = new ArrayList<>();
for(int i=0;ideck,int n){
int deckSize = deck.size();
List handView = deck.subList(deckSize-n,deckSize);
List hand = new ArrayList(handView);
handView.clear();
return hand;
}
}
结果:
请输入玩家人数: 5 请输入每手牌的牌数: 5 [K of ♥, 9 of ♥, K of ♠, Q of ♦, 4 of ♦] [Q of ♥, 4 of ♠, 2 of ♠, 9 of ♠, Q of ♠] [8 of ♣, 3 of ♠, 3 of ♣, 6 of ♣, 3 of ♥] [10 of ♣, 3 of ♦, 10 of ♠, 6 of ♠, 9 of ♣] [Q of ♣, 10 of ♥, 7 of ♣, 6 of ♦, J of ♠] 进程已结束,退出代码0
好了,一个简单的发牌程序就写成了。如果同学想提升自己的话可以升级一下,例如在控制台下进行出牌等操作,就像斗地主那种。
同学们,看完来个免费的点赞和关注吧!!谢谢了!!!



