gpt4 book ai didi

java - 创建一副牌时出现编译错误

转载 作者:行者123 更新时间:2023-12-01 13:19:50 25 4
gpt4 key购买 nike

我正在尝试创建一副纸牌,但在访问使用 ArrayList.get 方法创建的纸牌时遇到一些问题,我不确定是否已正确创建纸牌的 ArrayList。

卡片类别:

package cardgame;

public class Card {

private String number;
private String suit;

public Card(String number, String suit) {
this.number = number;
this.suit = suit;
}

public void setNumber(String number) {
this.number = number;
}

public void setSuite(String suit) {
this.suit = suit;
}

public String getNumber() {
return number;
}

public String getSuit() {
return suit;
}

}

牌组等级:

package cardgame;

import java.util.*;

public class Deck {

private String[] numberList = {"Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
private String[] suitList = {"Hearts", "Diamonds", "Clubs", "Spades"};

private ArrayList cardDeck = new ArrayList();

public ArrayList Deck() {
for(int i = 0; i < 4; i++) {
for(int j = 0; j < 13; j++) {
cardDeck.add(new Card(numberList[j], suitList[i]));
}
}
return cardDeck;
}

}

主类:

package cardgame;

public class CardGame {

public static void main(String[] args) {
Deck newDeck = new Deck();
System.out.println(newDeck.get(1));
}

}

在主类中,我只是使用 newDeck.get 来检索数据以查看 ArrayList 是否实际已填充。我收到以下指向该行的错误:

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: <any>
at cardgame.CardGame.main(CardGame.java:7)
Java Result: 1

最佳答案

我会让 Card 类不可变,因为一旦分配了数字和花色,它们就永远不会改变。或者你在现实生活中玩过纸牌游戏吗?无论哪种方式,您都必须定义这两个参数。

final 关键字表示,一旦将值分配给变量,就不能再更改。您还可以节省几行代码。

这是Card 类:

package cardgame;

public class Card {

private final String number;
private final String suit;

public Card(String number, String suit) {
this.number = number;
this.suit = suit;
}


public String getNumber() {
return number;
}

public String getSuit() {
return suit;
}

// Convenient for System.out.println()
@Override
public String toString() { return suit + " " + number; }

}

toString()是为每个Object声明的方法,并返回该类的字符串表示形式。我只是重新声明了这个表示的样子。您可以在下面的 main 方法中进一步了解我这样做的原因。

现在进入Deck类:

package cardgame;

import java.util.*;
// The Deck is a list of cards, so let it inherit from AbstractList
// Deck will be a Read-only list with all the syntactic sugar of lists then
public class Deck extends AbstractList {

private String[] numberList = {"Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
private String[] suitList = {"Hearts", "Diamonds", "Clubs", "Spades"};

private ArrayList<Card> cardDeck = new ArrayList<>();


// Constructor
public Deck() {
for(String number : numberList) {
for(String suit : suitList) {
cardDeck.add(new Card(number, suit));
}
}
}

@Override
public Card get(int i) { return cardDeck.get(i); }

@Override
public int size() { return cardDeck.size(); }

}

使用AbstractList ,您会在 Deck 内的卡片列表上看到某种查看。然后,您可以像普通列表一样使用该牌组(它是只读的,无需 add(Card)set(int, Card) 操作!)。

例如,在您的主类中:

package cardgame;

public class CardGame {

public static void main(String[] args) {
Deck newDeck = new Deck();
// Pick the card at index 1
System.out.println(newDeck.get(1));
// Iterate over all cards in the Deck and print them.
// This is basically sugar from AbstractList.
for(Card card : newDeck) {
System.out.println(card.toString());
}
}

}

关于java - 创建一副牌时出现编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22134900/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com