作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
因此,在我的 java 类(class)中,我们获得了这些纸牌和牌组类,这些类将在稍后的纸牌游戏中使用。这是卡代码:
public class Card {
// public constants:
public static final int ACE = 1;
public static final int DEUCE = 2;
public static final int TWO = 2;
public static final int THREE = 3;
public static final int FOUR = 4;
public static final int FIVE = 5;
public static final int SIX = 6;
public static final int SEVEN = 7;
public static final int EIGHT = 8;
public static final int NINE = 9;
public static final int TEN = 10;
public static final int JACK = 11;
public static final int KNAVE = 11;
public static final int QUEEN = 12;
public static final int KING = 13;
public static final int SPADES = 1;
public static final int HEARTS = 2;
public static final int DIAMONDS = 3;
public static final int CLUBS = 4;
// private instance data;
private int rank;
private int suit;
// public constructor:
public Card ( int rank, int suit ) {
if ( rank < Card.ACE | rank > Card.KING | suit < Card.SPADES | suit > Card.CLUBS ) {
throw new IllegalArgumentException();
} else {
this.rank = rank;
this.suit = suit;
}
}
/** Returns this card's suit. */
public int getSuit() {
return this.suit;
}
/** Returns this card's rank. */
public int getRank() {
return this.rank;
}
/** Returns a stringy version of this card. */
public String toString() {
// Replace the next instruction with your code:
throw new UnsupportedOperationException();
}
}
这是套牌类代码:
public class Deck {
// private instance data;
private Card[] cards;
// public constructor:
public Deck() {
this.cards = new Card[52];
int i = 0;
for ( int suit = Card.SPADES; suit <= Card.CLUBS; suit++ ) {
for ( int rank = Card.ACE; rank <= Card.KING; rank++ ) {
this.cards[i] = new Card(rank,suit);
i++;
}
}
}
/** Returns a copy of the card at the specified index in this deck. */
public Card cardAt(int index) {
if ( index < 0 | index > 51 ) {
throw new IllegalArgumentException();
} else {
return new Card( this.cards[index].getRank(),this.cards[index].getSuit() );
}
}
/** Shuffles this deck. */
public void shuffle() {
// Replace the next instruction with your code:
throw new UnsupportedOperationException();
}
/** Returns a stringy version of this deck. */
public String toString() {
// Replace the next instruction with your code:
throw new UnsupportedOperationException();
}
}
我的问题是我被困在两个类的 toString 方法和套牌类的 shuffle 方法上。有什么帮助吗?
最佳答案
shuffle可以实现如下
/** Shuffles this deck. */
public void shuffle() {
// Shuffle the elements in the array
Collections.shuffle(Arrays.asList(cards));
}
toString 只是构建一个字符串并返回它。这可以是您想要描述您的类(class)的任何内容。这就是我要做的。
对于甲板
/** Returns a stringy version of this deck. */
public String toString() {
String output="Current Deck:\n";
for (int i=0; i <cards.length ; i++){
output+= cardAt(i).toString();
}
return output;
}
至于卡,我会改变一些东西。您分配一堆整数来代表卡片,如果您不关心这些值,我个人会使用枚举。这样,具有数字以外的值的牌(即 J 与 10)将用其名称而不是某个数字来表示。谁真的说过“我有 12 支梅花”。我会把我的东西改成这个。
public enum Rank{ACE,TWO,THREE ....SO ON}
public enum Suit {HEARTS,CLUBS,SPADES,DIAMONDS}
然后您将更改所有的 get 方法,但真正的优势在于您不需要很长的 switch 语句来从枚举值推断出卡名称。你可以像这样实现 toString 。
//change rank and suit type from int to the Enums
Rank rank;
Suit suit;
/*modify all your getters*/
/** Returns a stringy version of this deck. */
public String toString() {
//enum will be printed out!
return rank+" of " + suit +"\n";
}
关于java - 卡牌和牌组弦乐和洗牌,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15822063/
我在 Rails 应用程序中有一个花哨的“工作表”样式 View ,加载时间太长。 (在开发模式下,是的,我知道那里没有缓存,“在 57893 毫秒内完成(查看:54975,DB:855)”)工作表是
我是一名优秀的程序员,十分优秀!