課程
/后端開發(fā)
/Java
/Java入門第三季
求簡易撲克牌小游戲的答案參考
2017-04-23
源自:Java入門第三季 7-1
正在回答
public class PokerDemo {
public static void main(String[] args) {
// 創(chuàng)建一個(gè)HashMap集合
HashMap<Integer, String> hm = new HashMap<Integer, String>();
// 創(chuàng)建一個(gè)ArrayList集合
ArrayList<Integer> array = new ArrayList<Integer>();
// 創(chuàng)建花色數(shù)組和點(diǎn)數(shù)數(shù)組
// 定義一個(gè)花色數(shù)組
String[] colors = { "?", "?", "?", "?" };
// 定義一個(gè)點(diǎn)數(shù)數(shù)組
String[] numbers = { "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q",
"K", "A", "2", };
// 從0開始往HashMap里面存儲(chǔ)編號(hào),并存儲(chǔ)對(duì)應(yīng)的牌,同時(shí)往ArrayList里面存儲(chǔ)編號(hào)即可。
int index = 0;
for (String number : numbers) {
for (String color : colors) {
String poker = color.concat(number);
hm.put(index, poker);
array.add(index);
index++;
}
hm.put(index, "小王");
hm.put(index, "大王");
// 洗牌(洗的是編號(hào))
Collections.shuffle(array);
// 發(fā)牌(發(fā)的也是編號(hào),為了保證編號(hào)是排序的,就創(chuàng)建TreeSet集合接收)
TreeSet<Integer> fengQingYang = new TreeSet<Integer>();
TreeSet<Integer> linQingXia = new TreeSet<Integer>();
TreeSet<Integer> liuYi = new TreeSet<Integer>();
TreeSet<Integer> diPai = new TreeSet<Integer>();
for (int x = 0; x < array.size(); x++) {
if (x >= array.size() - 3) {
diPai.add(array.get(x));
} else if (x % 3 == 0) {
fengQingYang.add(array.get(x));
} else if (x % 3 == 1) {
linQingXia.add(array.get(x));
} else if (x % 3 == 2) {
liuYi.add(array.get(x));
// 看牌(遍歷TreeSet集合,獲取編號(hào),到HashMap集合找對(duì)應(yīng)的牌)
lookPoker("風(fēng)清揚(yáng)", fengQingYang, hm);
lookPoker("林青霞", linQingXia, hm);
lookPoker("劉意", liuYi, hm);
lookPoker("底牌", diPai, hm);
// 寫看牌的功能
public static void lookPoker(String name, TreeSet<Integer> ts,
HashMap<Integer, String> hm) {
System.out.print(name + "的牌是:");
for (Integer key : ts) {
String value = hm.get(key);
System.out.print(value + " ");
System.out.println();
舉報(bào)
Java中你必須懂得常用技能,不容錯(cuò)過的精彩,快來加入吧
5 回答簡易撲克牌游戲程序!
4 回答簡易撲克牌游戲代碼
2 回答撲克牌游戲
3 回答簡易撲克牌游戲【3個(gè)晚上2小時(shí),暴力簡單。】
2 回答簡易的撲克牌
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號(hào)-11 京公網(wǎng)安備11010802030151號(hào)
購課補(bǔ)貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動(dòng)學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號(hào)
2017-04-24
public class PokerDemo {
public static void main(String[] args) {
// 創(chuàng)建一個(gè)HashMap集合
HashMap<Integer, String> hm = new HashMap<Integer, String>();
// 創(chuàng)建一個(gè)ArrayList集合
ArrayList<Integer> array = new ArrayList<Integer>();
// 創(chuàng)建花色數(shù)組和點(diǎn)數(shù)數(shù)組
// 定義一個(gè)花色數(shù)組
String[] colors = { "?", "?", "?", "?" };
// 定義一個(gè)點(diǎn)數(shù)數(shù)組
String[] numbers = { "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q",
"K", "A", "2", };
// 從0開始往HashMap里面存儲(chǔ)編號(hào),并存儲(chǔ)對(duì)應(yīng)的牌,同時(shí)往ArrayList里面存儲(chǔ)編號(hào)即可。
int index = 0;
for (String number : numbers) {
for (String color : colors) {
String poker = color.concat(number);
hm.put(index, poker);
array.add(index);
index++;
}
}
hm.put(index, "小王");
array.add(index);
index++;
hm.put(index, "大王");
array.add(index);
// 洗牌(洗的是編號(hào))
Collections.shuffle(array);
// 發(fā)牌(發(fā)的也是編號(hào),為了保證編號(hào)是排序的,就創(chuàng)建TreeSet集合接收)
TreeSet<Integer> fengQingYang = new TreeSet<Integer>();
TreeSet<Integer> linQingXia = new TreeSet<Integer>();
TreeSet<Integer> liuYi = new TreeSet<Integer>();
TreeSet<Integer> diPai = new TreeSet<Integer>();
for (int x = 0; x < array.size(); x++) {
if (x >= array.size() - 3) {
diPai.add(array.get(x));
} else if (x % 3 == 0) {
fengQingYang.add(array.get(x));
} else if (x % 3 == 1) {
linQingXia.add(array.get(x));
} else if (x % 3 == 2) {
liuYi.add(array.get(x));
}
}
// 看牌(遍歷TreeSet集合,獲取編號(hào),到HashMap集合找對(duì)應(yīng)的牌)
lookPoker("風(fēng)清揚(yáng)", fengQingYang, hm);
lookPoker("林青霞", linQingXia, hm);
lookPoker("劉意", liuYi, hm);
lookPoker("底牌", diPai, hm);
}
// 寫看牌的功能
public static void lookPoker(String name, TreeSet<Integer> ts,
HashMap<Integer, String> hm) {
System.out.print(name + "的牌是:");
for (Integer key : ts) {
String value = hm.get(key);
System.out.print(value + " ");
}
System.out.println();
}
}