使用JAVA编写以下程序:
创新互联公司是一家朝气蓬勃的网站建设公司。公司专注于为企业提供信息化建设解决方案。从事网站开发,网站制作,网站设计,网站模板,微信公众号开发,软件开发,小程序设计,10年建站对LED显示屏等多个领域,拥有多年的网站营销经验。
1、请定义一个名为Card的扑克牌类,该类有两个private访问权限的字符串变量face和suit;分别描述一张牌的牌面值(如:A、K、Q、J、10、9、……3、2等)和花色(如:“黑桃”、“红桃”、“梅花”、“方块”);定义Card类中的public访问权限的构造方法,为类中的变量赋值;定义protected访问权限的方法getFace(),得到扑克牌的牌面值;定义protected访问权限的方法getSuit(),得到扑克牌的花色;定义方法toString(),返回表示扑克牌的花色和牌面值字符串(如“红桃A”、“梅花10”等)。
2、若应用程序的main方法中,定义字符串数组f和s;分别表示扑克牌的牌面值和花色;定义52个元素的Card类型数组deck,用来存放4个花色的52张牌。如下所示。
String f[] = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
String s[] = {"黑桃","红桃","梅花","方块"};
Card deck = new Card[52];
(1)使用Card类的构造方法给deck数组的52张牌赋值,要求数组中先存放黑桃花色的A、2、3、……、K;然后是红桃花色的A、2、3、……、K;梅花花色的A、2、3、……、K;方块花色的A、2、3、……、K。请写出实现上述功能的程序段。
(2)请编写模拟洗牌的完整程序,即把数组deck中的扑克牌随机打乱存放顺序。
原题如下:
引用内容
private String face; // A, K, Q, J, 10, ... 2
private String suit; // Spade, Heart, Club, Diamond
public Card(String suit, String face) {
this.face = face;
this.suit = suit;
}
protected String getFace() {
return face;
}
protected String getSuit() {
return suit;
}
public String toString() {
return suit + " " + face;
}
解题,完成程序解:
程序代码
package poker;
public class Card {
private String face; // A, K, Q, J, 10, ... 2
private String suit; // Spade, Heart, Club, Diamond
public Card(String suit, String face) {
this.face = face;
this.suit = suit;
}
protected String getFace() {
return face;
}
protected String getSuit() {
return suit;
}
public String toString() {
return suit + " " + face;
}
public static void shuffle(Card[] deck, int startIndex, int size,
int splitIndex) { //shuffle洗牌
System.out.println("startIndex:" + startIndex);
if (splitIndex * 2 size) { //if rand is 30
Card.swap(deck, startIndex, splitIndex, size - splitIndex); //[52]、0、30、22
shuffle(deck, size - splitIndex, splitIndex, size - splitIndex); //[52]、22、30、22
} else if (splitIndex * 2 size) { //if rnd is 10
Card.swap(deck, startIndex, size - splitIndex, splitIndex); //[52]、0、42、10
shuffle(deck, startIndex, size - splitIndex, splitIndex); //[52]、0、42、10
} else {
Card.swap(deck, startIndex, splitIndex, splitIndex);
}
}
public static void swap(Card[] deck, int srcIndex, int dstIndex, int size) { //交换
//SRC是在本位置显示:source的缩写,源的意思 HREF是点击后连接的目标:HyperlinkReference,超链接引用
String face = "";
String suit = "";
for (int i = 0; i size; i++) {
face = deck[srcIndex + i].face;
suit = deck[srcIndex + i].suit;
deck[srcIndex + i].face = deck[dstIndex + i].face;
deck[srcIndex + i].suit = deck[dstIndex + i].suit;
deck[dstIndex + i].face = face;
deck[dstIndex + i].suit = suit;
}
}
/**
* @param args
*/
public static void main(String[] args) {
//第一问解答
Card[] deck = new Card[52];
String f[] = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J",
"Q", "K"};
String s[] = {"Spade", "Heart", "Club", "Diamond"};
for (int i = 0; i s.length; i++) {
for (int j = 0; j f.length; j++) {
deck[i * 13 + j] = new Card(s[i], f[j]); //依次存入数组之中,每13张一回合
}
}
//第二问解答
int rnd = 0;
int numOfShuffle = 10;
for (int i = 0; i numOfShuffle; i++) {
rnd = (int) Math.abs(Math.random() * 52); //Math.random()返回大于或等于 0.0 但小于 1.0 的伪随机 double 值。
Card.shuffle(deck, 0, deck.length, rnd);
}
// Test
/*
for (int i=0; ideck.length; i++)
{
System.out.println(deck[i]);
} */
}
}
代码仅供参考,如有疑问,欢迎追问:
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
* 随机扑克牌
* @author STONE
* @version 2015-10-28
*/
public class Poker {
//用于记录丢失的手牌数
private static Integer lostTotal = 0;
//用于存放已下发的扑克
private static Integer[] its1 = new Integer[54];
//用于存放当前的扑克
private static Integer[] its2 = new Integer[54];
private static ListInteger lostList = new ArrayListInteger();
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
for(int i=0;i54;i++){
its1[i] = 0;
its2[i] = 0;
}
System.out.println("系统开始发牌...");
givePoker(5);
System.out.println("发牌完毕,当前手牌为:");
printPoker(its2);
Integer gameTotal = 0;
while(gameTotal10){//循环跑10次,请注意此处发牌没做限制,如果循环次数过大,会导致死循环
System.out.println("请输入需要丢弃的手牌(多张牌格式如下:黑桃1,黑桃2,黑桃3):");
String lost = scn.nextLine();
checkLost(lost);
System.out.println("丢失手牌完毕,当前手牌:");
printPoker(its2);
System.out.println("系统开始重发手牌...");
givePoker(lostTotal);
System.out.println("重发发牌完毕,当前手牌为:");
printPoker(its2);
gameTotal++;
}
scn.close();
}
public static void givePoker(Integer num){//随机分发手牌
int total = 0;
while(totalnum){
int poke = (int)(Math.random()*54);
if(its1[poke] == 0){
its1[poke] = 1;
its2[poke] = 1;
total++;
}
}
lostTotal = 0;
}
public static Boolean checkLost(String lost){//检查需要丢失的手牌
if(lost==null || lost.equals("")){
System.out.println("输入手牌错误,系统无法处理...");
return false;
}
if(lost.indexOf("黑桃")0 lost.indexOf("红心")0 lost.indexOf("梅花")0 lost.indexOf("方块")0){
System.out.println("输入手牌错误,系统无法处理...");
return false;
}
String[] strs = lost.split(",");
Boolean bol = false;
for(String str : strs){
str = str.trim();
if(str==null || str.equals("")) continue;//去掉,防止空格
bol = getPokerNum(str);
}
if(bol){
losePoker();
}
return false;
}
public static void losePoker(){//丢掉手牌
lostTotal = lostList.size();
for(Integer itr : lostList){//丢掉手牌
its2[itr-1] = 0;
}
lostList = new ArrayListInteger();
}
public static Boolean getPokerNum(String str){//获取手牌点数并去掉
try{
Integer itr = 0;
if(str.indexOf("黑桃")==0){
str = str.replace("黑桃", "");
itr = Integer.parseInt(str);
if(itr13){
System.out.println("输入手牌不存在:黑桃"+str);
return false;
}
if(its2[itr-1]==1){
lostList.add(itr);
}
} else if(str.indexOf("红心")==0){
str = str.replace("红心", "");
itr = Integer.parseInt(str);
if(itr13){
System.out.println("输入手牌不存在:红心"+str);
return false;
}
if(its2[itr+12]==1){
lostList.add(itr+13);
}
} else if(str.indexOf("梅花")==0){
str = str.replace("梅花", "");
itr = Integer.parseInt(str);
if(itr13){
System.out.println("输入手牌不存在:梅花"+str);
return false;
}
if(its2[itr+25]==1){
lostList.add(itr+26);
}
} else if(str.indexOf("方块")==0){
str = str.replace("方块", "");
itr = Integer.parseInt(str);
if(itr13){
System.out.println("输入手牌不存在:方块"+str);
return false;
}
if(its2[itr+38]==1){
lostList.add(itr+39);
}
} else if(str.indexOf("小王")==0){
if(its2[52]==1){
lostList.add(53);
}
} else if(str.indexOf("大王")==0){
if(its2[53]==1){
lostList.add(54);
}
}
return true;
} catch( Exception e ){
System.out.println("输入手牌有误...");
return false;
}
}
public static void printPoker(Integer[] its){//打印当前手牌
String result = "";
for(int i=0;iits.length;i++){
if(its[i]==1){//等于1表示当前手牌存在
result+=pukerCheck(i+1)+",";
}
}
System.out.println(result);
}
public static String pukerCheck(Integer itr){//判断扑克类型
/**
* 1~13黑桃花色、14~26红心花色、27~39梅花花色
* 40~52方块花色、53小王、54大王
*/
if(1=itr itr=13){
return "黑桃"+itr;
} else if(14=itr itr=26){
return "红心"+(itr-13);
} else if(27=itr itr=39){
return "梅花"+(itr-26);
} else if(40=itr itr=52){
return "方块"+(itr-39);
} else if(itr==53){
return "小王";
} else if(itr==54){
return "大王";
}
return "";
}
}
测试运行结果:
package zhidao;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
/**
* 发牌游戏
*/
public class PokerGame
{
/**
* 共有1幅扑克牌,不包括大王和小王共52张牌。
*/
public static final int POKER_NUMBER = 52;
/**
* 特殊分隔符 _
*/
private static final String T = "_";
/**
* 52张牌br /
* 顺序为黑桃、红心、草花、方块
*/
public static final String[] POKERS =
{
"A" + T + "0", "A" + T + "1", "A" + T + "2", "A" + T + "3",
"2" + T + "0", "2" + T + "1", "2" + T + "2", "2" + T + "3",
"3" + T + "0", "3" + T + "1", "3" + T + "2", "3" + T + "3",
"4" + T + "0", "4" + T + "1", "4" + T + "2", "4" + T + "3",
"5" + T + "0", "5" + T + "1", "5" + T + "2", "5" + T + "3",
"6" + T + "0", "6" + T + "1", "6" + T + "2", "6" + T + "3",
"7" + T + "0", "7" + T + "1", "7" + T + "2", "7" + T + "3",
"8" + T + "0", "8" + T + "1", "8" + T + "2", "8" + T + "3",
"9" + T + "0", "9" + T + "1", "9" + T + "2", "9" + T + "3",
"10" + T + "0", "10" + T + "1", "10" + T + "2", "10" + T + "3",
"J" + T + "0", "J" + T + "1", "J" + T + "2", "J" + T + "3",
"Q" + T + "0", "Q" + T + "1", "Q" + T + "2", "Q" + T + "3",
"K" + T + "0", "K" + T + "1", "K" + T + "2", "K" + T + "3"
};
/**
* 原始的扑克牌
*/
public static final String[] ORIAL_POKERS = POKERS.clone ();
/**
* 发到哪张牌 0
*/
public static int currentIndex = 0;
/**
* 四种花色
*/
public static final String POKER_COLOR = "黑桃" + T + "红心" + T + "草花" + T + "方块";
/**
* 发牌
* @param n int 一共几个人玩牌
* @param results 储存发牌结果的数组
*/
public static void drawPoker (int n, String[][] results)
{
int m = n - 1;
if (n == 0)
{
return;
}
int number = (POKER_NUMBER - currentIndex) / n;
while (number 0)
{
String current = POKERS[currentIndex++];
String pk = current.split ("\\" + T)[0].trim ();
int col = Integer.parseInt (current.split ("\\" + T )[1].trim ());
if (null == results[m][col])
{
results[m][col] = pk + " ";
}
else
{
results[m][col] += pk + " ";
}
number--;
}
for ( int i = 0; i results.length; i++ )
{
for ( int j = 0; j results[i].length; j++ )
{
if (null == results[i][j])
{
results[i][j] = "";
}
}
}
drawPoker (--n, results);
}
/**
* 给牌排序
* @param orialPokers String 原始扑克牌
* @param temp String[] 每张花色的扑克牌数组
*/
private static void sortPokers ( final String orialPokers, String[] temp )
{
Arrays.sort (temp, new ComparatorString ()
{
@Override
public int compare ( String o1, String o2 )
{
int index1 = orialPokers.indexOf (o1 + T);
int index2 = orialPokers.indexOf (o2 + T);
if (index1 index2)
{
return -1;
}
else if (index1 index2)
{
return 1;
}
else
{
return 0;
}
}
});
}
/**
* 洗牌
*/
private static void shufflePokers ( String[] POKERS )
{
Collections.shuffle (Arrays.asList (POKERS));
}
public static void main ( String[] args )
{
System.out.print ("输入人数:");
Scanner scanner = new Scanner (System.in);
int n = scanner.nextInt ();
scanner.close ();
String[] pColors = POKER_COLOR.split ("\\" + T );
String[][] results = new String[n][pColors.length];
shufflePokers (POKERS);
drawPoker (n, results);
final String poker = Arrays.toString (ORIAL_POKERS);
System.out.println ("\n输出如下:\n");
for (int i = 0; i results.length; i++)
{
System.out.println ("第" + ( i + 1 ) + "个人:");
for ( int j = 0; j results[i].length; j++ )
{
String[] temp = results[i][j].trim ().split ("\\s+");
sortPokers (poker, temp);
System.out.println (pColors[j] + ":" + Arrays.toString (temp).replaceAll ("[\\,\\[\\]]", ""));
}
System.out.println ();
}
}
}
1.定义扑克的对象 有两个参数a。花色,b数字Poke{ int number, String color, Poker(String color,int number) { this.color=color; this.number=number; }} 2.每一张牌都实例化一个扑克对象。 poke1= new Poke("红桃","10");poke2=new Poke("黑桃","9"); 3.定义游戏规则类,类里定义一个方法,用来比较大小。public comparePoke(Poke poke1,Poke poke2){ if(花色比较){ return 结果; } if(数字比较){ return 结果; }}。调用方法就能比较
扑克牌还是自己写个对象比较好吧 属性就是花色跟牌值
至于发牌什么的 还是用集合来吧 数组操作起来比较麻烦
代码实在是懒得写了 就提点建议
用集合的好处比如说洗牌直接就可以用 collections的静态方法 还有就是 回复初始状态排序 也可以也可以自定义比较器来比较