对象错误的 Java 数组(卡)



让我们开始,3个类(卡,开始,一副牌)

1º 卡:

public class Card {
private String type;
private int value;
private String number;

public Card(String number,String type,int value) {
this.type=type;
this.value=value;
this.number=number;
}
public String gettype() {
return type;
}
public String getNumber() {
return number;
}
public int getValue() {
return value;
}
@Override
public String toString() {
return getNumber() + " de " + gettype() + "  --> VALUE " + getValue() ;
}
}

2º 启动

public class Start {
public static void main(String[] args) {
Deckofcards deckofcards = new Deckofcards();
Card c = deckofcards.newCard();
String s =c.toString();
System.out.println(s);
Card c2 = deckofcards.newCard();
String s2 =c2.toString();
System.out.println(s2);
}
}

那么,问题就在这里

套牌:

public class Deckofcards {
private Card CardsR[];
private final Card[] Cards = {
new Card("A","heart",1),
new Card("2","heart",2),
new Card("3","heart",3),
new Card("4","heart",4),
new Card("5","heart",5),
new Card("6","heart",6),
new Card("7","heart",7),
new Card("8","heart",8),
new Card("9","heart",9),
new Card("10","heart",10),
new Card("J","heart",10),
new Card("Q","heart",10),
new Card("K","heart",10),
new Card("As","Diamond",1),
new Card("2","Diamond",2),
new Card("3","Diamond",3),
new Card("4","Diamond",4),
new Card("5","Diamond",5),
new Card("6","Diamond",6),
new Card("7","Diamond",7),
new Card("8","Diamond",8),
new Card("9","Diamond",9),
new Card("10","Diamond",10),
new Card("J","Diamond",10),
new Card("Q","Diamond",10),
new Card("K","Diamond",10),
new Card("A","clover",1),
new Card("2","clover",2),
new Card("3","clover",3),
new Card("4","clover",4),
new Card("5","clover",5),
new Card("6","clover",6),
new Card("7","clover",7),
new Card("8","clover",8),
new Card("9","clover",9),
new Card("10","clover",10),
new Card("J","clover",10),
new Card("Q","clover",10),
new Card("K","clover",10),
new Card("A","Spades",1),
new Card("2","Spades",2),
new Card("3","Spades",3),
new Card("4","Spades",4),
new Card("5","Spades",5),
new Card("6","Spades",6),
new Card("7","Spades",7),
new Card("8","Spades",8),
new Card("9","Spades",9),
new Card("10","Spades",10),
new Card("J","Spades",10),
new Card("Q","Spades",10),
new Card("K","Spades",10),
};
//Sorry if the translation its not correct, this is only a little part of a 
//big code.
public Deckofcards() {
Collections.shuffle(Arrays.asList(Cards)); //SHUFFLE
CardsR=Cards.clone(); //COPY
}
public Card newCard(){
boolean while1 = true;
Card take=null;
for(int i = 0; i < CardsR.length; i++){
while(while1){
if(CardsR[i]!=null){
take=Cards[i];
//CardsR[i]=null;
while1=false;
}
}
}
return take;
}
}

让我们解释一下。

我有一系列卡片,我混合在一起。 好的,全部正确

然后我打电话来抄卡(类:开始)。 好的,全部正确

---然后--- 当我再次调用时,卡返回相同的值... 我试图设置一个空...但随后它无缘无故地开始了一个无限循环?.

有什么可能的解决方案吗? 谢谢

当我再次调用时,卡返回相同的值

因为

for(int i = 0; i < CardsR.length; i++){  // i = 0 
while(while1){                       // true
if(CardsR[i]!=null){                 // true
take=Cards[i];                   //
while1=false;                    // so no more while loop execution 
// first card will always returned

我试图设置一个空...但随后它开始了一个无限循环 原因?

在第一次调用期间Card c = deckofcards.newCard();CardsR[0]设置为 null,因此在第二次调用期间Card c2 = deckofcards.newCard();您的ifif(CardsR[i]!=null)条件将永远不会执行,现在您陷入了无限while循环

boolean while1 = true;
Card take=null;
for(int i = 0; i < CardsR.length; i++){
while(while1){
if(CardsR[i]!=null){
take=Cards[i];
//CardsR[i]=null;
while1=false;
}
}
}

解决方案:只需Random实例即可从数组中选取并返回随机cards

如何从数组中随机选择一个元素

你并没有真正对洗牌列表做任何事情;该实例本质上是暂时的,因此是垃圾回收。请注意,将您创建的列表从数组中洗牌不会打乱数组。相反,您可以这样做:

List<Card> cardList = Arrays.asList(Cards);
Collections.shuffle(cardList);
CardsR = cardList.toArray(new Card[cardList.size()]);

您的newCard方法不需要这么复杂。我假设您只想从洗牌数组中返回一张卡。一种方法是维护最后一张牌的索引;您可以将其初始化为在构造函数中-1。在newCard中,只要您不越界,您将递增此索引并在该索引处返回卡。如果是,您可能需要打印一条消息,指出没有剩余的卡片(例如)。

其他一些指针:

  • 确保遵循 Java 命名约定;字段和变量名称不应大写。
  • 考虑使用List<Card>牌而不是数组;这样你就可以简单地洗牌,而不是在数组和列表之间不必要地来回转换。

take的值将始终是数组中的第一张卡。

将值设置为 null 时,while循环永远不会终止while2因为只有在找到非 null 条目时才设置为false。一旦找到null,则什么都不会发生,while循环将永远运行,因为无法将while2设置为false

break语句应该可以在这里解决问题

public Card newCard(){
Card take=null;
for(int i = 0; i < CardsR.length; i++){
if(CardsR[i]!=null){
take=Cards[i];
CardsR[i]=null;
break;
}
}
}
return take;
}

break语句将退出它所在的任何循环。

或者,您可以使用List<Card>而不是数组,您可以使用数组从随机列表中删除条目并返回它们,而无需设置和检查null.

private List<Card> cardList;
public Deckofcards() {
cardList = Collections.shuffle(Arrays.asList(Cards)); //SHUFFLE
}
public Card newCard(){
if(!cardList.isEmpty()){
return cardList.remove(0);        
}            
return null;
}

相关内容

  • 没有找到相关文章

最新更新