本实验实现为玩家分一幅扑克牌,先为扑克洗牌再分牌
实现花色和点数 的分牌,玩家人数控制台输入
看看结果
package card;
import java.util.*;
class Pocker {
private String[] Pocker = new String[52];
private String[] num = new String[]{"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
private String[] flower = new String[] {"黑桃","红桃","梅花","方块"};
private String[] boss={"小王","大王"};
public Pocker () {
int n = 0;
for (int i = 0; i
for (int j = 0; j
Pocker[n++] = (flower[i] + num[j]);
}
}
}
public void print () {
for(int i = 1; i <= Pocker.length; i++) {
System.out.printf("%5s",Pocker[i -1] );
if (i % num.length == 0) {
System.out.println ();
}
}
}
public void divide(int n)
{
for (int i = 1; i < n+1; i++)
{
System.out.println( "第"+i+"个人的牌为:");
for (int j = 1+(int)(Pocker.length/n)*(i-1); j < (int)(Pocker.length/n)*i; j++)
{
System.out.printf("%5s",Pocker[j]);
if((j)%5==0)
System.out.println ();
}
System.out.println();
}
System.out.println("多余的牌");
for (int i = Pocker.length-Pocker.length%n; i
{
System.out.printf("%5s",Pocker[i]);
}
}
public void shuffle () {
for(int i = 0; i < Pocker.length; i++) {
String temp = Pocker[i];
int n = (int)(Math.random() * Pocker.length);
Pocker[i] = Pocker[n];
Pocker[n] = temp;
}
}
}
public class TestCard {
public static void main (String[] args) {
Pocker m1 = new Pocker ();
System.out.println("洗牌前");
m1.print ();
System.out.println("洗牌后");
m1.shuffle();
m1.print();
Scanner in=new Scanner(System.in);
System.out.println("输入分给多少人");
int n=in.nextInt();
m1.divide(n);
}
} 游戏逻辑挺简单,自己看代码吧