用java程序编写一个发牌的程序。。。跪求了。。。。
答案:4 悬赏:0 手机版
解决时间 2021-11-10 13:41
- 提问者网友:留有余香
- 2021-11-09 23:51
用java程序编写一个发牌的程序。。。跪求了。。。。
最佳答案
- 五星知识达人网友:人间朝暮
- 2021-11-09 23:59
分少,一点动力都没有,念你初学,给你写一个:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Admin {
public static void main(String... args) {
Deck deck = new Deck();
deck.reset();
deck.shuffle();
for (int i = 0; i < 53; i++) {
deck.drawCard();
}
}
}
class Card {
private String value;
private String suit;
public String getSuit() {
return suit;
}
public void setSuit(String suit) {
this.suit = suit;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String toString() {
return suit + value;
}
}
class Deck {
private String[] huase = { "红桃", "黑桃", "方片", "梅花" };
private String[] value = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" };
private Card[] card = null;
private List randomList = new ArrayList();
private int idx = 0;
public void reset() {
List data = new ArrayList();
for (int i = 0; i < huase.length; i++) {
for (int j = 0; j < value.length; j++) {
Card c = new Card();
c.setSuit(huase[i]);
c.setValue(value[j]);
data.add(c);
}
}
card = (Card[]) data.toArray(new Card[0]);
System.out.println(Arrays.deepToString(card));
}
public void shuffle() {
while (randomList.size() < 52) {
int i = (int) (Math.random() * 52);
if (!randomList.contains(i)) {
randomList.add(i);
}
}
for (int i = 0; i < randomList.size(); i++) {
int idx = Integer.parseInt(String.valueOf(randomList.get(i)));
System.out.print(card[idx] + ",");
}
System.out.println();
}
public void drawCard() {
if (idx >= 52) {
System.out.println("已发完毕");
} else {
int a = Integer.parseInt(String.valueOf(randomList.get(idx++)));
System.out.println(card[a]);
}
}
}
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Admin {
public static void main(String... args) {
Deck deck = new Deck();
deck.reset();
deck.shuffle();
for (int i = 0; i < 53; i++) {
deck.drawCard();
}
}
}
class Card {
private String value;
private String suit;
public String getSuit() {
return suit;
}
public void setSuit(String suit) {
this.suit = suit;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String toString() {
return suit + value;
}
}
class Deck {
private String[] huase = { "红桃", "黑桃", "方片", "梅花" };
private String[] value = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" };
private Card[] card = null;
private List randomList = new ArrayList();
private int idx = 0;
public void reset() {
List data = new ArrayList();
for (int i = 0; i < huase.length; i++) {
for (int j = 0; j < value.length; j++) {
Card c = new Card();
c.setSuit(huase[i]);
c.setValue(value[j]);
data.add(c);
}
}
card = (Card[]) data.toArray(new Card[0]);
System.out.println(Arrays.deepToString(card));
}
public void shuffle() {
while (randomList.size() < 52) {
int i = (int) (Math.random() * 52);
if (!randomList.contains(i)) {
randomList.add(i);
}
}
for (int i = 0; i < randomList.size(); i++) {
int idx = Integer.parseInt(String.valueOf(randomList.get(i)));
System.out.print(card[idx] + ",");
}
System.out.println();
}
public void drawCard() {
if (idx >= 52) {
System.out.println("已发完毕");
} else {
int a = Integer.parseInt(String.valueOf(randomList.get(idx++)));
System.out.println(card[a]);
}
}
}
全部回答
- 1楼网友:山君与见山
- 2021-11-10 02:51
5分?
- 2楼网友:你哪知我潦倒为你
- 2021-11-10 01:56
1、设计二维数组a[13][4],13代表2,3,4,5,...,K,A , 4代表红黑方梅 13*4=52
2、这样a[0][0]就是红桃2,a[12][3]就是梅花A
按照这么来随机发牌就很简单了,直接随机个一个a[i][j]就可以了。i和j都分别在13和4以内
思路明确了,代码还是自己写比较好
2、这样a[0][0]就是红桃2,a[12][3]就是梅花A
按照这么来随机发牌就很简单了,直接随机个一个a[i][j]就可以了。i和j都分别在13和4以内
思路明确了,代码还是自己写比较好
- 3楼网友:大漠
- 2021-11-10 01:37
也写一个 用静态方法表示了类
public class Deck {
static String[] values=new String[]{"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
static String[] suits=new String[]{"红桃","黑桃","方片","梅花"};
static int vl=values.length;
static int sl=suits.length;
static int dl=vl*sl;
static String[] decks=new String[dl];
public static String get(int s,int v){
return suits[s]+"-"+values[v];
}
public static void reset(){
for(int i=0,k=0;i for(int j=0;j decks[k++]=get(j,i);
}
}
}
public static void shuffle(){
Random rdm=new Random();
String s;
for(int i=0,j;i
s=decks[j];
decks[j]=decks[i];
decks[i]=s;
}
}
public static void drawCard(int i){
System.out.println(decks[i]);
}
public static void main(String[] args) {
reset();
for(int i=0;i
}
System.out.println("-------洗牌-------");
shuffle();
for(int i=0;i
}
}
}
public class Deck {
static String[] values=new String[]{"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
static String[] suits=new String[]{"红桃","黑桃","方片","梅花"};
static int vl=values.length;
static int sl=suits.length;
static int dl=vl*sl;
static String[] decks=new String[dl];
public static String get(int s,int v){
return suits[s]+"-"+values[v];
}
public static void reset(){
for(int i=0,k=0;i
}
}
}
public static void shuffle(){
Random rdm=new Random();
String s;
for(int i=0,j;i
s=decks[j];
decks[j]=decks[i];
decks[i]=s;
}
}
public static void drawCard(int i){
System.out.println(decks[i]);
}
public static void main(String[] args) {
reset();
for(int i=0;i
}
System.out.println("-------洗牌-------");
shuffle();
for(int i=0;i
}
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯