感谢大家的帮助!!这就是我的目标。
import java.util.Arrays;公共课 甲板{
String [] cards = {"AH", "2H", "3H", "4H", "5H", "6H", "7H", "8H",
“9H”,“10H”,“JH”,“QH”,“KH”, “AC”,“2C”,“3C”,“4C”,“5C”,“6C”,“7C”,“8C”,“9C”, “10C”,“JC”,“QC”,“KC”, “AD”,“2D”,“3D”,“4D”,“5D”,“6D”,“7D”,“8D”,“9D”, “10D”,“JD”,“QD”,“KD”, “AS”,“2S”,“3S”,“4S”,“5S”,“6S”,“7S”,“8S”,“9S”, “10S”,“JS”,“QS”,“KS”, };
Deck(){ } public void shuffle() { String [] temp = new String[52]; for ( int i = 0; i < 26; i++){ temp [2*i] = cards[i]; temp [2*i+1]= cards[i+26]; } cards = temp; } @Override public String toString() { String cards1 = ""; for ( int i = 0; i < cards.length; i++){ cards1 += cards[i] + " "; if ((i+1)%13==0){ cards1 += "\n"; } } return cards1; } public boolean equals(Deck other) { for (int i=0; i<cards.length; ++i) { if (!this.cards[i].equals(other.cards[i]))
返回false; } 返回true; }
}
嗨,我的实验室需要一些帮助。
B中。据说如果一副纸牌被给予足够的完美洗牌次数,它将恢复原来的顺序。通过将甲板完全分成两半并将卡片从两半交叉来完成一次完美的洗牌;也就是说,第一张牌是从上半场开始,第二张牌是从上半场开始,第三张牌是从上半场开始,依此类推。
我需要包含以下方法。 -Deck()构造函数,它创建一个未洗过的甲板。 - 一个shuffle()方法,可以完美地进行随机播放。 -A toString()方法打印卡组 - 等于(Deck aDeck)方法,将自己与给定牌组进行比较,如果两个牌组中的所有牌牌都处于相同的顺序,则返回true,否则返回
我认为我的构造函数部分存在问题。我不知道如何为字符串数组创建正确的构造函数。我有三本java书,但都没有涉及它。
公共课DeckTester {
/** * @param args the command line arguments */ public static void main(String[] args) { Deck d1 = new Deck(); System.out.println(d1); } } public class Deck { String [] cards = {"AH", "2H", "3H", "4H", "5H", "6H", "7H", "8H",
“9H”,“10H”,“JH”,“QH”,“KH”, “AC”,“2C”,“3C”,“4C”,“5C”,“6C”,“7C”,“8C”, “9C”,“10C”,“JC”,“QC”,“KC”, “AD”,“2D”,“3D”,“4D”,“5D”,“6D”,“7D”,“8D”, “9D”,“10D”,“JD”,“QD”,“KD”, “AS”,“2S”,“3S”,“4S”,“5S”,“6S”,“7S”,“8S”, “9S”,“10S”,“JS”,“QS”,“KS”, };
甲板(){ cards = new String [] {}; }
public void shuffle() { for ( int i = 0; i< cards.length; i++){ String temp = cards[ i ]; // swap cards[ i ] = cards[ i+25 ]; // the cards[ i+25 ] = temp; // cards } } }
答案 0 :(得分:3)
可以假设卡片将始终相同。所以你可以有一个常量来定义可用的卡片组。
然后,您的构造函数只需分配内存并将常量数组复制到此成员变量。
public class Deck {
// static final defines a constant valid for all the objects of that class.
// this will allow us to initiase our array in a clean way.
// You will notice that constants are always named with capital letters.
private static final String[] ORDERED_CARDS = new String[] {
"AH", "2H", "3H", "4H", "5H", "6H", "7H", "8H",
"9H", "10H", "JH"
// etc.
};
// This is the member variable that holds the cards for our Deck objects
private String[] cards;
// The constructor allocates memory with the new keyword
// Then it uses a function from the Java library to copy from one array to the other
public Deck() {
cards = new String[ORDERED_CARDS.length];
System.arraycopy(ORDERED_CARDS, 0, cards, 0, ORDERED_CARDS.length);
}
// Using Google "java shuffle array" you'll find how to do it in one line using the
// Java library. There are other ways to do it, depending on which shuffle type
// you need. You could as well implement your own algorithm. The class to generate
// random numbers is called Random (part of the Java library)
public void shuffle() {
assert(cards!=null);
Collections.shuffle(Arrays.asList(cards));
}
// The toString method shows how to iterate over an array or a collection using the
// "for-each" loop. We use a StringBuffer to build a string with the resulting card
// list. The final keyword says that this variable will not be re-allocated anywhere
// else during this method. This avoids mistakes in long methods and also allows the
// Java compiler to optimize your code
public String toString() {
final StringBuffer sb = new StringBuffer();
for (String card : cards) {
sb.append(card);
sb.append(", ");
}
return sb.toString();
}
// The equals method shows how to iterate over an array or a collection using the
// "for" loop.
public boolean equals(Deck other) {
if (other==null) return false;
// assert is used to ensure that a Deck will always have cards. If not, the program
// will throw an Exception. This is good practise when you write classes to assert
// that you don't get values that should not be possible.
assert(other.cards!=null);
// Decks can only be equal if they have the same number of cards
if (other.cards.length != this.cards.length) return false;
final int cardCount = this.cards.length;
for (int i=0; i<cardCount; ++i) {
// Always compare strings using the equals method, not ==
if (!this.cards[i].equals(other.cards[i])) return false;
}
// If we reach this point, then we have equal objects
return true;
}
}
答案 1 :(得分:1)
编辑:我不相信使代码变得比它需要的更复杂。
public class Deck {
private final String[] cards = "AH,2H,3H,4H,5H,6H,7H,8H,9H,10H,JH,QH,KH,AC,2C,3C,4C,5C,6C,7C,8C,9C,10C,JC,QC,KC,AD,2D,3D,4D,5D,6D,7D,8D,9D,10D,JD,QD,KD,AS,2S,3S,4S,5S,6S,7S,8S,9S,10S,JS,QS,KS".split(",");
// the default constructor will do everything, nothing needs to be added.
public void shuffle() {
Collections.shuffle(Arrays.asList(cards));
}
public String toString() {
return Arrays.toString(cards);
}
// equals must extend equals(Object) or it won't do what you thing.
public boolean equals(Object other) {
return other instanceof Deck
&& Arrays.equals(cards, ((Deck) other).cards);
}
}
答案 2 :(得分:1)
这就是我认为你想要的。我遗漏了洗牌部分,没有给出全部答案:)
public class Deck {
public static void main(String[] args) {
Deck deck = new Deck();
System.out.println(deck);
String original = deck.toString();
while (true) {
deck.shuffle();
System.out.println(deck);
if (deck.toString().equals(original)) {
return;
}
}
}
String[] cards = { "AH", "2H", "3H", "4H", "5H", "6H", "7H", "8H", "9H",
"10H", "JH", "QH", "KH", "AC", "2C", "3C", "4C", "5C", "6C", "7C",
"8C", "9C", "10C", "JC", "QC", "KC", "AD", "2D", "3D", "4D", "5D",
"6D", "7D", "8D", "9D", "10D", "JD", "QD", "KD", "AS", "2S", "3S",
"4S", "5S", "6S", "7S", "8S", "9S", "10S", "JS", "QS", "KS", };
public void shuffle() {
// omitted
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
for (String card : cards) {
builder.append(card + " ");
}
return builder.toString();
}
}
答案 3 :(得分:0)
你的构造函数应该是这样的:
public class Deck {
private String[] cards = new String[52];
public Deck() {
cards[0] = "AH";
cards[1] = "2H";
cards[2] = "3H";
cards[3] = "4H";
cards[4] = "5H";
cards[5] = "6H";
//and so on
}
}