我最近开始尝试自学一些基本的Java。我决定参与的第一个项目之一是一个简单的二十一点程序。我正在为游戏创建一些基本结构(我还没有对程序的实际游戏组件做任何事情)而且我还没有能够让我的shuffleDeck
方法返回值。
我查看了各种互联网资源,甚至与其他一些功能性二十一点程序交叉引用,我仍然无法找到我的错误。当我运行main方法时,它应该洗牌并将其打印出来。有人可以帮忙吗?我有一个卡片类,一个甲板类和一个主要的二十一点类。它们是分开的,但我把它们全部集中在一起。
public class Card {
private String suit;
private String name;
private int value;
public Card(String nameOfCard, int gameValue,String nameOfSuit){
value = gameValue;
suit = nameOfSuit;
name = nameOfCard;
}
String getSuit(){
return suit;
}
String getName(){
return name;
}
int getValue(){
return value;
}
}
public class Deck {
private Card[] cardDeck;
public Deck(){
cardDeck = new Card[52];
cardDeck[0]= new Card("Two",2,"Diamonds");
cardDeck[1]= new Card("Three",3,"Diamonds");
cardDeck[2]= new Card("Four",4,"Diamonds");
cardDeck[3]= new Card("Five",5,"Diamonds");
cardDeck[4]= new Card("Six",6,"Diamonds");
cardDeck[5]= new Card("Seven",7,"Diamonds");
cardDeck[6]= new Card("Eight",8,"Diamonds");
cardDeck[7]= new Card("Nine",9,"Diamonds");
cardDeck[8]= new Card("Ten",10,"Diamonds");
cardDeck[9]= new Card("Jack",10,"Diamonds");
cardDeck[10]= new Card("Queen",10,"Diamonds");
cardDeck[11]= new Card("King",10,"Diamonds");
cardDeck[12]= new Card("Ace",11,"Diamonds");
cardDeck[13]= new Card("Two",2,"Hearts");
cardDeck[14]= new Card("Three",3,"Hearts");
cardDeck[15]= new Card("Four",4,"Hearts");
cardDeck[16]= new Card("Five",5,"Hearts");
cardDeck[17]= new Card("Six",6,"Hearts");
cardDeck[18]= new Card("Seven",7,"Hearts");
cardDeck[19]= new Card("Eight",8,"Hearts");
cardDeck[20]= new Card("Nine",9,"Hearts");
cardDeck[21]= new Card("Ten",10,"Hearts");
cardDeck[22]= new Card("Jack",10,"Hearts");
cardDeck[23]= new Card("Queen",10,"Hearts");
cardDeck[24]= new Card("King",10,"Hearts");
cardDeck[25]= new Card("Ace",11,"Hearts");
cardDeck[26]= new Card("Two",2,"Clubs");
cardDeck[27]= new Card("Three",3,"Clubs");
cardDeck[28]= new Card("Four",4,"Clubs");
cardDeck[29]= new Card("Five",5,"Clubs");
cardDeck[30]= new Card("Six",6,"Clubs");
cardDeck[31]= new Card("Seven",7,"Clubs");
cardDeck[32]= new Card("Eight",8,"Clubs");
cardDeck[33]= new Card("Nine",9,"Clubs");
cardDeck[34]= new Card("Ten",10,"Clubs");
cardDeck[35]= new Card("Jack",10,"Clubs");
cardDeck[36]= new Card("Queen",10,"Clubs");
cardDeck[37]= new Card("King",10,"Clubs");
cardDeck[38]= new Card("Ace",11,"Clubs");
cardDeck[39]= new Card("Two",2,"Spades");
cardDeck[40]= new Card("Three",3,"Spades");
cardDeck[41]= new Card("Four",4,"Spades");
cardDeck[42]= new Card("Five",5,"Spades");
cardDeck[43]= new Card("Six",6,"Spades");
cardDeck[44]= new Card("Seven",7,"Spades");
cardDeck[45]= new Card("Eight",8,"Spades");
cardDeck[46]= new Card("Nine",9,"Spades");
cardDeck[47]= new Card("Ten",10,"Spades");
cardDeck[48]= new Card("Jack",10,"Spades");
cardDeck[49]= new Card("Queen",10,"Spades");
cardDeck[50]= new Card("King",10,"Spades");
cardDeck[51]= new Card("Ace",11,"Spades");
}
public void shuffleDeck(){
Card temp;
int num;
for(int k = 0;k<3000;k++)
{
num = (int)Math.random()*52;
temp = cardDeck[num];
cardDeck[num]=cardDeck[0];
cardDeck[0]=temp;
}
}
void printDeck(){
for(int t = 0;t<52;t++){
System.out.println(cardDeck[t].getName() + " of " + cardDeck[t].getSuit());
}
}
}
public class BlackJack {
public static void main(String[] args){
Deck cards = new Deck();
cards.shuffleDeck();
cards.printDeck();
}
}
答案 0 :(得分:1)
我看到的第一件事是(int)Math.random()*52
将总是评估为0.你基本上生成一个介于0和1之间的数字,切断逗号之后的所有内容(通过强制转换为int),所以你总是得到一个0然后你乘以52无效。
你想要(int)(Math.random()*52)
。这样你首先乘以然后再施法。更好的是,使用Random类生成所需范围内的数字,或者更好的是,只需改为Collections.shuffle(Arrays.asList(cardDeck));
。
答案 1 :(得分:0)
首先我会尝试使用:
Collections.shuffle(Arrays.asList(cardDeck));
而不是实现shuffle方法。为什么重新发明轮子?
答案 2 :(得分:0)
这一行失败了:num = (int)Math.random()*52;
就像你立刻投了一样!
将其更改为num = (int) (Math.random() * 52);
这是固定版本:
public class Card {
private String suit;
private String name;
private int value;
public Card(String nameOfCard, int gameValue,String nameOfSuit){
value = gameValue;
suit = nameOfSuit;
name = nameOfCard;
}
String getSuit(){
return suit;
}
String getName(){
return name;
}
int getValue(){
return value;
}
}
class Deck {
private Card[] cardDeck;
public Deck(){
cardDeck = new Card[52];
cardDeck[0]= new Card("Two",2,"Diamonds");
cardDeck[1]= new Card("Three",3,"Diamonds");
cardDeck[2]= new Card("Four",4,"Diamonds");
cardDeck[3]= new Card("Five",5,"Diamonds");
cardDeck[4]= new Card("Six",6,"Diamonds");
cardDeck[5]= new Card("Seven",7,"Diamonds");
cardDeck[6]= new Card("Eight",8,"Diamonds");
cardDeck[7]= new Card("Nine",9,"Diamonds");
cardDeck[8]= new Card("Ten",10,"Diamonds");
cardDeck[9]= new Card("Jack",10,"Diamonds");
cardDeck[10]= new Card("Queen",10,"Diamonds");
cardDeck[11]= new Card("King",10,"Diamonds");
cardDeck[12]= new Card("Ace",11,"Diamonds");
cardDeck[13]= new Card("Two",2,"Hearts");
cardDeck[14]= new Card("Three",3,"Hearts");
cardDeck[15]= new Card("Four",4,"Hearts");
cardDeck[16]= new Card("Five",5,"Hearts");
cardDeck[17]= new Card("Six",6,"Hearts");
cardDeck[18]= new Card("Seven",7,"Hearts");
cardDeck[19]= new Card("Eight",8,"Hearts");
cardDeck[20]= new Card("Nine",9,"Hearts");
cardDeck[21]= new Card("Ten",10,"Hearts");
cardDeck[22]= new Card("Jack",10,"Hearts");
cardDeck[23]= new Card("Queen",10,"Hearts");
cardDeck[24]= new Card("King",10,"Hearts");
cardDeck[25]= new Card("Ace",11,"Hearts");
cardDeck[26]= new Card("Two",2,"Clubs");
cardDeck[27]= new Card("Three",3,"Clubs");
cardDeck[28]= new Card("Four",4,"Clubs");
cardDeck[29]= new Card("Five",5,"Clubs");
cardDeck[30]= new Card("Six",6,"Clubs");
cardDeck[31]= new Card("Seven",7,"Clubs");
cardDeck[32]= new Card("Eight",8,"Clubs");
cardDeck[33]= new Card("Nine",9,"Clubs");
cardDeck[34]= new Card("Ten",10,"Clubs");
cardDeck[35]= new Card("Jack",10,"Clubs");
cardDeck[36]= new Card("Queen",10,"Clubs");
cardDeck[37]= new Card("King",10,"Clubs");
cardDeck[38]= new Card("Ace",11,"Clubs");
cardDeck[39]= new Card("Two",2,"Spades");
cardDeck[40]= new Card("Three",3,"Spades");
cardDeck[41]= new Card("Four",4,"Spades");
cardDeck[42]= new Card("Five",5,"Spades");
cardDeck[43]= new Card("Six",6,"Spades");
cardDeck[44]= new Card("Seven",7,"Spades");
cardDeck[45]= new Card("Eight",8,"Spades");
cardDeck[46]= new Card("Nine",9,"Spades");
cardDeck[47]= new Card("Ten",10,"Spades");
cardDeck[48]= new Card("Jack",10,"Spades");
cardDeck[49]= new Card("Queen",10,"Spades");
cardDeck[50]= new Card("King",10,"Spades");
cardDeck[51]= new Card("Ace",11,"Spades");
}
public void shuffleDeck(){
Card temp;
int num;
for(int k = 0;k<3000;k++)
{
num = (int) (Math.random() * 52);
temp = cardDeck[num];
cardDeck[num]=cardDeck[0];
cardDeck[0]=temp;
}
}
void printDeck(){
for(int t = 0;t<52;t++){
System.out.println(cardDeck[t].getName() + " of " + cardDeck[t].getSuit());
}
}
}
public class BlackJack {
public static void main(String[] args){
Deck cards = new Deck();
cards.shuffleDeck();
cards.printDeck();
}
}