我如何洗牌甲板?怎样使一个字符串数组构造? [英] How do I shuffle a deck? How do I make a string array constructor?
问题描述
感谢您对大家的帮助!这是我跟去了。
2011年3月30日 -
进口java.util.Arrays中;公共类
甲板{的String []卡= {啊,2H,3H,4H,5H,6H,7H,8H
9H,10H,JH,QH,KH
交流,2C,3C,4C,5C,6C,7C,8C,9C,
10℃,赛马,QC,KC
AD,2D,3D,4D,5D,6D,7D,8D,9D,
10D,JD,量子点,KD,
AS,2S,3S,4S,5S,6S,7S,8S,9,
10S,爵士,QS,KS,
};甲板(){}
公共无效洗牌()
{
的String [] TEMP =新的String [52];
的for(int i = 0; I< 26;我++){
温度[2 * i] =卡[I]
温度[2 * I + 1] =卡[I + 26]。
} 卡=温度; }@覆盖
公共字符串的toString(){ 串cards1 =;
的for(int i = 0; I< cards.length;我++){
cards1 + =卡[I] +;
如果(第(i + 1)%13 == 0){
cards1 + =\\ n;
}
}
返回cards1;}
公共布尔等于(甲板等){
的for(int i = 0; I< cards.length ++我){
如果(!this.cards [I] .equals(other.cards [I]))
返回false;
}
返回true;
}
}
块引用>我需要用我的实验室HW一些帮助。
乙。有人说,如果一副扑克牌被赋予完美的洗牌足够的时间,就会恢复到原来的顺序。一个完美的洗牌是由分裂甲板恰好一半,从交错的两半卡进行;即,第一卡是从第一半中,第二从第二半从第一半第三等
我需要包括下面的方法。
创建一个unshuffled甲板-Deck()构造函数。
-A洗牌()方法,做一个完美的洗牌。
-A toString()方法打印在甲板
-an的equals(甲板aDeck)方法,它本身带有给他甲板进行比较,如果两个卡座的所有卡都以相同的顺序并返回true,否则为false我觉得我有构造的一部分问题。我不知道如何创建一个字符串数组一个正确的构造函数。我有三个电子书,其中没有触及它。
公共类DeckTester {
/ **
* @参数ARGS的命令行参数
* /
公共静态无效的主要(字串[] args){
甲板D1 =新甲板(); 的System.out.println(D1); }
}公共类甲板{的String [] =卡{啊,2H,3H,4H,5H,6H,7H,8H
9H,10H,JH,QH,KH
交流,2C,3C,4C,5C,6C,7C,8C,
9C,10C,赛马,QC,KC
AD,2D,3D,4D,5D,6D,7D,8D,
9D,10D,JD,量子点,KD,
AS,2S,3S,4S,5S,6S,7S,8S
9S,10S,爵士,QS,KS,
};
甲板(){
卡=新的String [] {}; }公共无效洗牌()
{
的for(int i = 0; I< cards.length;我++){
字符串TEMP =卡[I] //交换
卡[I] =卡[I + 25]。 //将
卡第[i + 25 =温度; //卡
}
}}
块引用>解决方案人们可以假定卡将始终是相同的。所以,你可以有一个定义可用的套牌恒定的。
您构造然后简单地分配内存和拷贝常量数组这个成员变量。
公共类甲板{
//静态最终定义了一个恒定的有效期为该类的所有对象。
//这将使我们能够initiase我们的阵列中的清洁方式。
//你会发现,常量总是用大写字母命名。
私有静态最后的String [] = ORDERED_CARDS新的String [] {
啊,2H,3H,4H,5H,6H,7H,8H
9H,10H,JH
//等等。
}; //这是存放在卡我们的甲板对象的成员变量
私有String []卡; //构造函数分配使用new关键字的内存
//然后,它使用一个函数从Java库从一个阵列复制到其他
公共甲板(){
卡=新的String [ORDERED_CARDS.length]
System.arraycopy(ORDERED_CARDS,0,卡,0,ORDERED_CARDS.length);
} //使用谷歌的java数组洗牌,你会发现如何使用做一行
// Java库。还有其他的方法来做到这一点,这取决于洗牌型
// 你需要。你可以同时实现自己的算法。类生成
//随机数称为随机(Java库的一部分)
公共无效洗牌(){
断言(卡!= NULL);
Collections.shuffle(Arrays.asList(卡));
} // toString方法展示了如何使用遍历数组或一个集合
//的for-each循环。我们用一个StringBuffer建立与生成的卡串
//列表。 final关键字表示,这个变量将不会在任何地方重新分配
//此方法的过程中别人。这避免了在长的方法的错误,并且还允许
// Java编译器来优化您的code
公共字符串的toString(){
最后的StringBuffer某人=新的StringBuffer();
对于(字符串卡:卡){
sb.append(卡);
sb.append(,);
}
返回sb.toString();
} // equals方法展示了如何使用遍历数组或一个集合
//for循环。
公共布尔等于(甲板等){
如果(其他== NULL)返回false; //断言用来确保桥面总是有卡。如果不是,程序
//会抛出异常。这是很好的做法,当你写的类断言
//你没有得到的值不应该是可能的。
断言(other.cards!= NULL); //甲板只能等于如果它们具有相同数量的卡
如果(other.cards.length = this.cards.length!)返回false; 最终诠释cardCount = this.cards.length;
的for(int i = 0; I< cardCount ++我){
//始终比较使用equals方法字符串,而不是==
如果返回false(this.cards [I] .equals(other.cards [I])!);
} //如果我们达到这一点,那么我们有相等的对象
返回true;
}
}Thank you for everyone's help!! This is what I went with.
3/30/2011-
import java.util.Arrays; 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", };
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]))
return false; } return true; }
}
Hi, I need some assistance with my lab hw.
B. It is said that if a deck of cards is given perfect shuffles enough times, it will return to its original order. A perfect shuffle is done by splitting the deck exactly in half and interleaving the cards from the two halves; that is, the first card is from the first half, the second from the second half, the third from the first half and so on.
I need to include the following methods. -Deck() constructor that creates an unshuffled deck. -A shuffle() method that does a perfect shuffle. -A toString() method that print the deck -An equals(Deck aDeck) method that compares itself with he given deck and returns true if all the cards in both decks are in the same order and false otherwise
I think I am having problems with the constructor part. I don't know how to create a correct constructor for string arrays. I have three java books, and none of them touched on it.
public class 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", };
Deck(){ 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 } } }
解决方案One could assume that the cards will always be the same. So you could have a constant that defines the set of cards available.
Your constructor then simply allocates memory and copies the constant array to this member variable.
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; } }
这篇关于我如何洗牌甲板?怎样使一个字符串数组构造?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!