Java问题 高分求解!!!!!!
首先定义类Card,它表示扑克牌的点数,如”Ace”,”Deuce”,…,”Jack”,”Queen”,”King”,扑克牌的花数“Hearts(红桃)”,“Diamon...
首先定义类Card,它表示扑克牌的点数,如”Ace”,”Deuce”,…,”Jack”,”Queen”,”King”,扑克牌的花数“Hearts(红桃)”,“Diamonds(方块)”,“Clubs(梅花)”,“Spades(黑桃)”。然后定义类DeckOfCards,它创建52张牌的一副扑克牌,每个元素都是一个Card对象。最后构建一个测试程序,显示类DeckOfCards的洗牌和发牌的功能。 这是老师的作业 题我看不懂 哪位高人帮忙解决下 谢了
展开
3个回答
展开全部
import java.util.Random;
/**
* 实现输出52张扑克牌 分给四个人A B C D
*/
public class Poker {
public static void main(String args[]) throws Exception {
Random random = new Random();
int[] poker = getPoker();
int j = 0;
int temp = 0;
for (int i = 0; i < 52; i++) {
temp = poker[i];
j = random.nextInt(52);
poker[i] = poker[j];
poker[j] = temp;
}
String A = "A:";
String B = "B:";
String C = "C:";
String D = "D:";
for (int i = 0; i < 52; i++) {
if (i % 4 == 0) {
A += getColor(poker[i]) + format(poker[i] % 13) + "\t";
}
if (i % 4 == 1) {
B += getColor(poker[i]) + format(poker[i] % 13) + "\t";
}
if (i % 4 == 2) {
C += getColor(poker[i]) + format(poker[i] % 13) + "\t";
}
if (i % 4 == 3) {
D += getColor(poker[i]) + format(poker[i] % 13) + "\t";
}
}
System.out.println(A);
System.out.println();
System.out.println(B);
System.out.println();
System.out.println(C);
System.out.println();
System.out.println(D);
}
// 通过数字的大小来断定颜色 从而实现 每种牌都有13张
public static String getColor(int i) {
if ((i - 1) / 13 == 0) {
return "红桃";
} else if ((i - 1) / 13 == 1) {
return "黑桃";
} else if ((i - 1) / 13 == 2) {
return "方块";
} else {
return "梅花";
}
}
// 输出格式 此处实现 1--10正常输出 11,12,0 输出对应的牌号
public static String format(int i) {
if (i == 11) {
return "J";
} else if (i == 12) {
return "Q";
} else if (i == 0) {
return "K";
} else {
return String.valueOf(i);
}
}
// 获得一副扑克
public static int[] getPoker() {
int[] poker = new int[52];
for (int i = 0; i < 52; i++) {
poker[i] = i + 1;
}
return poker;
}
}
/**
* 实现输出52张扑克牌 分给四个人A B C D
*/
public class Poker {
public static void main(String args[]) throws Exception {
Random random = new Random();
int[] poker = getPoker();
int j = 0;
int temp = 0;
for (int i = 0; i < 52; i++) {
temp = poker[i];
j = random.nextInt(52);
poker[i] = poker[j];
poker[j] = temp;
}
String A = "A:";
String B = "B:";
String C = "C:";
String D = "D:";
for (int i = 0; i < 52; i++) {
if (i % 4 == 0) {
A += getColor(poker[i]) + format(poker[i] % 13) + "\t";
}
if (i % 4 == 1) {
B += getColor(poker[i]) + format(poker[i] % 13) + "\t";
}
if (i % 4 == 2) {
C += getColor(poker[i]) + format(poker[i] % 13) + "\t";
}
if (i % 4 == 3) {
D += getColor(poker[i]) + format(poker[i] % 13) + "\t";
}
}
System.out.println(A);
System.out.println();
System.out.println(B);
System.out.println();
System.out.println(C);
System.out.println();
System.out.println(D);
}
// 通过数字的大小来断定颜色 从而实现 每种牌都有13张
public static String getColor(int i) {
if ((i - 1) / 13 == 0) {
return "红桃";
} else if ((i - 1) / 13 == 1) {
return "黑桃";
} else if ((i - 1) / 13 == 2) {
return "方块";
} else {
return "梅花";
}
}
// 输出格式 此处实现 1--10正常输出 11,12,0 输出对应的牌号
public static String format(int i) {
if (i == 11) {
return "J";
} else if (i == 12) {
return "Q";
} else if (i == 0) {
return "K";
} else {
return String.valueOf(i);
}
}
// 获得一副扑克
public static int[] getPoker() {
int[] poker = new int[52];
for (int i = 0; i < 52; i++) {
poker[i] = i + 1;
}
return poker;
}
}
展开全部
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Test {
public static void main(String[] args) {
// 创建一幅牌
DeckOfCards cards = new DeckOfCards();
// 未洗牌前,先看看牌是什么样子的
cards.deal();
// 洗牌
cards.shuffle();
// 洗牌后,先看看牌是什么样子的
cards.deal();
}
}
class Card {
// 这里用整型来代表花色与点数,point(0-12代表点数A-K),suit(0-3代表花色),表示再说
private int point, suit;
public Card(int point, int suit) {
this.point = point;
this.suit = suit;
}
// 点数对应的英文
public static final String[] POINTS = { "ace", "deuce", "three", "four",
"five", "six", "severn", "eight", "nine", "ten", "jack", "queen",
"king" };
// 花色对应的英文
public static final String[] SUITS = { "heart", "spades", "diamonds",
"club" }; // 0123分别对应红黑方梅
// 打印出这张扑克,包括花色与点数
public String toString() {
return SUITS[suit] + " " + POINTS[point];
}
}
// 一幅牌
class DeckOfCards {
List<Card> cards = new ArrayList<Card>();
public DeckOfCards() {
// 创建52张牌
for (int suit = 0; suit < 4; suit++) {// 花色
for (int point = 0; point < 13; point++) {
cards.add(new Card(point, suit));
}
}
}
/**
* 洗牌
*/
public void shuffle() {
Collections.shuffle(cards);
}
/**
* 发牌,其实就是显示
*/
public void deal() {
for (Card card : cards) {
System.out.println(card);
}
}
}
import java.util.Collections;
import java.util.List;
public class Test {
public static void main(String[] args) {
// 创建一幅牌
DeckOfCards cards = new DeckOfCards();
// 未洗牌前,先看看牌是什么样子的
cards.deal();
// 洗牌
cards.shuffle();
// 洗牌后,先看看牌是什么样子的
cards.deal();
}
}
class Card {
// 这里用整型来代表花色与点数,point(0-12代表点数A-K),suit(0-3代表花色),表示再说
private int point, suit;
public Card(int point, int suit) {
this.point = point;
this.suit = suit;
}
// 点数对应的英文
public static final String[] POINTS = { "ace", "deuce", "three", "four",
"five", "six", "severn", "eight", "nine", "ten", "jack", "queen",
"king" };
// 花色对应的英文
public static final String[] SUITS = { "heart", "spades", "diamonds",
"club" }; // 0123分别对应红黑方梅
// 打印出这张扑克,包括花色与点数
public String toString() {
return SUITS[suit] + " " + POINTS[point];
}
}
// 一幅牌
class DeckOfCards {
List<Card> cards = new ArrayList<Card>();
public DeckOfCards() {
// 创建52张牌
for (int suit = 0; suit < 4; suit++) {// 花色
for (int point = 0; point < 13; point++) {
cards.add(new Card(point, suit));
}
}
}
/**
* 洗牌
*/
public void shuffle() {
Collections.shuffle(cards);
}
/**
* 发牌,其实就是显示
*/
public void deal() {
for (Card card : cards) {
System.out.println(card);
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public class Card {
public Card() {
}
public Card(String pointStr,String style){
this.pointStr=pointStr;
this.style=style;
}
private String pointStr;
private String style; //样式
public String getPointStr() {
return pointStr;
}
public void setPointStr(String pointStr) {
this.pointStr = pointStr;
}
public String getStyle() {
return style;
}
public void setStyle(String style) {
this.style = style;
}
public String toString() {
return this.style+","+this.pointStr;
}
}
public class DeckOfCards {
public DeckOfCards() {
}
public static void main(String[] args){
String[] points=new String[]{"Ace","Deuce","Three","Four","Five","Six","Seven","Eight","Nine","Ten", "Jack","Queen","King"};
String[] styles=new String[]{"Hearts","Diamonds","Clubs","Spades"};
Card[] card=new Card[52];
for(int i=0;i<styles.length;i++){
for(int j=0;j<points.length;j++){
card[13*i+j]=new Card(points[j],styles[i]);
}
}
for(int i=0;i<card.length;i++){
System.out.println(card[i]);
}
}
}
public Card() {
}
public Card(String pointStr,String style){
this.pointStr=pointStr;
this.style=style;
}
private String pointStr;
private String style; //样式
public String getPointStr() {
return pointStr;
}
public void setPointStr(String pointStr) {
this.pointStr = pointStr;
}
public String getStyle() {
return style;
}
public void setStyle(String style) {
this.style = style;
}
public String toString() {
return this.style+","+this.pointStr;
}
}
public class DeckOfCards {
public DeckOfCards() {
}
public static void main(String[] args){
String[] points=new String[]{"Ace","Deuce","Three","Four","Five","Six","Seven","Eight","Nine","Ten", "Jack","Queen","King"};
String[] styles=new String[]{"Hearts","Diamonds","Clubs","Spades"};
Card[] card=new Card[52];
for(int i=0;i<styles.length;i++){
for(int j=0;j<points.length;j++){
card[13*i+j]=new Card(points[j],styles[i]);
}
}
for(int i=0;i<card.length;i++){
System.out.println(card[i]);
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询