1.定義撲克的對(duì)象 有兩個(gè)參數(shù)a?;ㄉ?,b數(shù)字Poke{ int number, String color, Poker(String color,int number) { this.color=color; this.number=number; }} 2.每一張牌都實(shí)例化一個(gè)撲克對(duì)象。 poke1= new Poke("紅桃","10");poke2=new Poke("黑桃","9"); 3.定義游戲規(guī)則類,類里定義一個(gè)方法,用來比較大小。public comparePoke(Poke poke1,Poke poke2){ if(花色比較){ return 結(jié)果; } if(數(shù)字比較){ return 結(jié)果; }}。調(diào)用方法就能比較
10年積累的做網(wǎng)站、成都網(wǎng)站建設(shè)經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶對(duì)網(wǎng)站的新想法和需求。提供各種問題對(duì)應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先網(wǎng)站制作后付款的網(wǎng)站建設(shè)流程,更有元江縣免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
public?class?PokerTest?{
public?static?void?main(String[]?args)?{
Poker.show();
}
}
class?Poker?{
public?int?num;
public?String?breed;
public?Poker()?{
}
public?static?void?show()?{
int?i?=?(int)?(Math.random()?*?13)?+?1;//?生成1-13的隨機(jī)數(shù)
String[]?s?=?new?String[]?{?"紅心",?"黑桃",?"方塊",?"黑花"?};
int?j?=?(int)?(Math.random()?*?4);
Poker?p?=?new?Poker();
p.num?=?i;
p.breed?=?s[j];
System.out.println(p.toString());
}
@Override
public?String?toString()?{
return?"Poker?[num="?+?num?+?",?breed="?+?breed?+?"]";
}
}
一:import java.util.Scanner;
public class Play {
/**
* 玩牌
*/
public static void main(String[] args) {
Poker poker = new Poker();
boolean over = false;
Scanner cin=new Scanner(System.in);
while(!over){
System.out.println("打牌輸入1,展示剩余牌輸入其他字符:");
String c=cin.nextLine();
if("1".equals(c)){
int index = (int) (Math.random()*poker.getSize());
poker.remove(index);
}else{
poker.ShowPages();
}
}
/*for(int i = 0;i54;i++){
int index = (int) (Math.random()*poker.getSize());
poker.remove(index);
poker.ShowPages();
}
*/
}
}二://牌
public class Page { private String huase = "";
private String haoma = "";
public Page(String huase,String haoma){
this.huase = huase;
this.haoma = haoma;
}
public String show(){
return this.huase+this.haoma;
}
}三:import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* 一副撲克
*/
public class Poker {
private List pages = new ArrayList();
public Poker(){
String huase[] = new String[]{"黑桃","紅桃","草花","方塊"};
String haoma[] = new String[]{"A","2","3","4","5","6","7","8","9","10"
,"J","Q","K"};
for(int i = 0;ihaoma.length;i++){
for(int j = 0;jhuase.length;j++){
pages.add(new Page(huase[j],haoma[i]));
}
}
pages.add(new Page("","大王"));
pages.add(new Page("","小王"));
}
public void ShowPages(){
System.out.println("展示剩余牌:");
for(Iterator i = pages.iterator();i.hasNext();){
System.out.println(((Page)i.next()).show());
}
}
//打牌
public void remove(int index){
pages.remove(index);
}
//剩余牌數(shù)
public int getSize(){
return pages.size();
}
}
使用JAVA編寫以下程序:
1、請(qǐng)定義一個(gè)名為Card的撲克牌類,該類有兩個(gè)private訪問權(quán)限的字符串變量face和suit;分別描述一張牌的牌面值(如:A、K、Q、J、10、9、……3、2等)和花色(如:“黑桃”、“紅桃”、“梅花”、“方塊”);定義Card類中的public訪問權(quán)限的構(gòu)造方法,為類中的變量賦值;定義protected訪問權(quán)限的方法getFace(),得到撲克牌的牌面值;定義protected訪問權(quán)限的方法getSuit(),得到撲克牌的花色;定義方法toString(),返回表示撲克牌的花色和牌面值字符串(如“紅桃A”、“梅花10”等)。
2、若應(yīng)用程序的main方法中,定義字符串?dāng)?shù)組f和s;分別表示撲克牌的牌面值和花色;定義52個(gè)元素的Card類型數(shù)組deck,用來存放4個(gè)花色的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類的構(gòu)造方法給deck數(shù)組的52張牌賦值,要求數(shù)組中先存放黑桃花色的A、2、3、……、K;然后是紅桃花色的A、2、3、……、K;梅花花色的A、2、3、……、K;方塊花色的A、2、3、……、K。請(qǐng)寫出實(shí)現(xiàn)上述功能的程序段。
(2)請(qǐng)編寫模擬洗牌的完整程序,即把數(shù)組deck中的撲克牌隨機(jī)打亂存放順序。
原題如下:
引用內(nèi)容
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是點(diǎn)擊后連接的目標(biāo):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]); //依次存入數(shù)組之中,每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 的偽隨機(jī) double 值。
Card.shuffle(deck, 0, deck.length, rnd);
}
// Test
/*
for (int i=0; ideck.length; i++)
{
System.out.println(deck[i]);
} */
}
}
當(dāng)前文章:java代碼撲克牌 java撲克牌順子
分享鏈接:http://m.rwnh.cn/article18/dosgddp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、品牌網(wǎng)站制作、、網(wǎng)站改版、網(wǎng)站導(dǎo)航、企業(yè)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)