作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
public class RandomCard {
int num;
int suitNum;
String cardSuit = new String("");
String cardNum = new String("");
String fullCard = new String("");
public RandomCard(){
num= (int)(Math.random()*13)+2;
suitNum =(int)(Math.random()*4)+1;
if(num == 11)
cardNum="Jack";
else if(num== 12)
cardNum="Queen";
else if(num== 13)
cardNum= "King";
else if(num== 14)
cardNum= "Ace";
else
cardNum= ""+num;
if(suitNum==1)
cardSuit= "Hearts";
else if(suitNum==2)
cardSuit= "Spades";
else if(suitNum==3)
cardSuit= "Clubs";
else
cardSuit= "Diamonds";
}
public String getNum(){
return cardNum;
}
public String getSuit(){
return cardSuit;
}
public String toString(){
return fullCard= cardNum+" of "+cardSuit;
}
}
public class RandomCardDecK {
ArrayList<RandomCard> deck = new ArrayList<RandomCard>();
String order = new String("");
public RandomCardDeck(){
while (deck.size()<52){
checkDeck(deck);
}
}
public ArrayList<RandomCard> checkDeck(ArrayList<RandomCard>Deck){
RandomCard card = new RandomCard();
for (int i=0; i<=Deck.size();i++){
if (card.equals(Deck.get(i))){
checkDeck(Deck);
}
else
Deck.add(card);
}
return Deck;
}
public String toString(){
for (int i=0; i<=deck.size();i++){
order += deck.get(i);
}
return order;
}
}
我正在尝试创建一副困惑的牌,然后显示该牌。我是编程新手,我不明白程序中的递归出了什么问题,但却引发了运行时错误。
最佳答案
编辑 2:我更新了答案,您现在可以检查结果,它运行良好
编辑3:添加了比较器,使结果更加清晰
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class RandomCard implements Comparator<RandomCard> {
int num;
int suitNum;
String cardSuit = "";
String cardNum = "";
public RandomCard() {
num = (int) (Math.random() * 13) + 2;
suitNum = (int) (Math.random() * 4) + 1;
if (num == 11) {
cardNum = "Jack";
} else if (num == 12) {
cardNum = "Queen";
} else if (num == 13) {
cardNum = "King";
} else if (num == 14) {
cardNum = "Ace";
} else {
cardNum = "" + num;
}
if (suitNum == 1) {
cardSuit = "Hearts";
} else if (suitNum == 2) {
cardSuit = "Spades";
} else if (suitNum == 3) {
cardSuit = "Clubs";
} else {
cardSuit = "Diamonds";
}
}
public int getNum() {
return num;
}
public int getSuitNum() {
return suitNum;
}
public String getCardSuit() {
return cardSuit;
}
public String getCardNum() {
return cardNum;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
RandomCard randomCard = (RandomCard) obj;
return this.getNum() == randomCard.getNum() && this.getSuitNum() == randomCard.getSuitNum();
}
@Override
public String toString() {
return cardNum + " of " + cardSuit;
}
@Override
public int compare(RandomCard o1, RandomCard o2) {
int c = new Integer(o1.getNum()).compareTo(o2.getNum());
if (c == 0) {
c = new Integer(o1.getSuitNum()).compareTo(o2.getSuitNum());
}
return c;
}
public static class RandomCardDeck {
ArrayList<RandomCard> deck;
String order = "";
public RandomCardDeck() {
deck = new ArrayList<>();
checkDeck(new RandomCard(), 0);
//your deck is now full with 52 cards
}
public ArrayList<RandomCard> getDeck() {
return deck;
}
private void checkDeck(RandomCard card, int i) {
if (deck.size() == 52) {
return;
}
if (i >= deck.size()) {
deck.add(card);
checkDeck(new RandomCard(), 0);
} else if (card.equals(deck.get(i))) {
checkDeck(new RandomCard(), 0);
} else {
checkDeck(card, ++i);
}
}
@Override
public String toString() {
for (int i = 0; i <= deck.size(); i++) {
order += deck.get(i);
}
return order;
}
}
public static void main(String[] args) {
RandomCardDeck cardDeck = new RandomCardDeck();
Collections.sort(cardDeck.getDeck(), new RandomCard());
for (RandomCard randomCard : cardDeck.getDeck()) {
System.out.println(randomCard);
}
}
}
结果:
run:
2 of Hearts
2 of Spades
2 of Clubs
2 of Diamonds
3 of Hearts
3 of Spades
3 of Clubs
3 of Diamonds
4 of Hearts
4 of Spades
4 of Clubs
4 of Diamonds
5 of Hearts
5 of Spades
5 of Clubs
5 of Diamonds
6 of Hearts
6 of Spades
6 of Clubs
6 of Diamonds
7 of Hearts
7 of Spades
7 of Clubs
7 of Diamonds
8 of Hearts
8 of Spades
8 of Clubs
8 of Diamonds
9 of Hearts
9 of Spades
9 of Clubs
9 of Diamonds
10 of Hearts
10 of Spades
10 of Clubs
10 of Diamonds
Jack of Hearts
Jack of Spades
Jack of Clubs
Jack of Diamonds
Queen of Hearts
Queen of Spades
Queen of Clubs
Queen of Diamonds
King of Hearts
King of Spades
King of Clubs
King of Diamonds
Ace of Hearts
Ace of Spades
Ace of Clubs
Ace of Diamonds
BUILD SUCCESSFUL (total time: 0 seconds)
关于java - 递归:为什么我会出错?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30145295/
我是一名优秀的程序员,十分优秀!