gpt4 book ai didi

java - 在 Java 中从另一个类调用 ArrayList 和方法很困难

转载 作者:行者123 更新时间:2023-12-01 07:23:56 25 4
gpt4 key购买 nike

第一次发帖,长期潜伏者。

我是一名计算机科学专业二年级学生,正在尝试编写二十一点游戏代码。这不是家庭作业,只是一个有趣的项目。由于尚未完成,因此游戏本身尚未准备好,因此请忽略该部分。我花了大约两个小时寻找答案,但无法将其他用户的类似问题与我自己的问题进行比较,因此我决定发布我的问题。

我的问题是“甲板”ArrayList。从我的 Blackjack.java 文件中,无论我做什么,我都无法调用它,我无法调用 Card 类中的任何方法,我无法操作它,我似乎无法在不收到某种错误的情况下执行任何操作。

public class Blackjack {
public int bet;
public int earnings = 0;
public int total = 100;
public boolean inputNeeded = true;

public Blackjack() {
Scanner s = new Scanner(System.in);

System.out.println(" Welcome to Blackjack.");
System.out.println(" Enter any key to continue.");
System.out.println(" Otherwise, please type Exit to quit.");

while (!s.nextLine().toString().toLowerCase().equals("exit")) //Main Game Loop - Only ends when user types Exit.
{
while (inputNeeded) {
System.out.println("You currently have $" + total + ".");
System.out.print("Please place your bets now. (Min $10): $");

if (s.hasNextInt()) {
bet = s.nextInt();
String betString = String.valueOf(bet);
inputNeeded = false;

if (bet < 10 || bet % 5 != 0) {
System.out.println("Your bet is not valid. Your bet must be a minimum of $10 and be a factor of 5.");
System.out.println("");
inputNeeded = true;
}
} else {
System.out.println("Your bet is not valid. Your bet must be a minimum of $10 and be a factor of 5.");
System.out.println("");
}
}
inputNeeded = true;

/*
* RESERVED SPACE (stuff to-do)
*
* Deal two cards to dealer and player.
* Flip cards, add totals
* If dealer has ace, prompt user if he wants insurance incase dealer blacjack
* Main loop, as long as neither player has blackjack, prompt user to hit, stay, double down etc.
* Once loop exits, calculate bets, add into totals.
* Increase count of how many games it's been, shuffle the decks at X games depending on howManyDecks (card.java)
*
* RESERVED SPACE (stuff to-do)
*
*/
deck.shuffle();

break; //until game is finished
}

System.out.print("Thank you for playing!");
}

public static void main(String[] args) {
Blackjack game = new Blackjack();
}
}

还有卡片类(我知道我可以循环完成 52 张卡片,但为了简单起见,我决定不这样做)。据我所知,如果我不想创建静态方法,我就必须创建“getter”方法。我不太确定返回方法将如何帮助我,例如,从 blackjack 类中洗牌,或者从 blackjack 类中抽牌(尚未实现),因为这些方法将 void 作为其返回类型。我什至不确定我的 getter 方法是否正确,因为我以前从未尝试过“获取”构造函数所需的参数。

public class Card
extends Blackjack //Do I need to extend this? <---- not sure
{
public String name;
public int value;
public String suit;
public int howManyDecks = 8; //most casino's use 8 decks for blackjack

public Card(String n, int v, String s) {
name = n;
value = v;
suit = s;

ArrayList<Card> discard = new ArrayList<Card>(); //this will be for any card that is not currently in the deck, I could have called it 'temp'
ArrayList<Card> deck = new ArrayList<Card>(howManyDecks * 52);

for (int i = 0; i < howManyDecks; i++) {
deck.add(new Card("Ace", 1, "Hearts"));
deck.add(new Card("2", 2, "Hearts"));
deck.add(new Card("3", 3, "Hearts"));
deck.add(new Card("4", 4, "Hearts"));
deck.add(new Card("5", 5, "Hearts"));
deck.add(new Card("6", 6, "Hearts"));
deck.add(new Card("7", 7, "Hearts"));
deck.add(new Card("8", 8, "Hearts"));
deck.add(new Card("9", 9, "Hearts"));
deck.add(new Card("10", 10, "Hearts"));
deck.add(new Card("Jack", 10, "Hearts"));
deck.add(new Card("Queen", 10, "Hearts"));
deck.add(new Card("King", 10, "Hearts"));
deck.add(new Card("Ace", 1, "Spades"));
deck.add(new Card("2", 2, "Spades"));
deck.add(new Card("3", 3, "Spades"));
deck.add(new Card("4", 4, "Spades"));
deck.add(new Card("5", 5, "Spades"));
deck.add(new Card("6", 6, "Spades"));
deck.add(new Card("7", 7, "Spades"));
deck.add(new Card("8", 8, "Spades"));
deck.add(new Card("9", 9, "Spades"));
deck.add(new Card("10", 10, "Spades"));
deck.add(new Card("Jack", 10, "Spades"));
deck.add(new Card("Queen", 10, "Spades"));
deck.add(new Card("King", 10, "Spades"));
deck.add(new Card("Ace", 1, "Diamonds"));
deck.add(new Card("2", 2, "Diamonds"));
deck.add(new Card("3", 3, "Diamonds"));
deck.add(new Card("4", 4, "Diamonds"));
deck.add(new Card("5", 5, "Diamonds"));
deck.add(new Card("6", 6, "Diamonds"));
deck.add(new Card("7", 7, "Diamonds"));
deck.add(new Card("8", 8, "Diamonds"));
deck.add(new Card("9", 9, "Diamonds"));
deck.add(new Card("10", 10, "Diamonds"));
deck.add(new Card("Jack", 10, "Diamonds"));
deck.add(new Card("Queen", 10, "Diamonds"));
deck.add(new Card("King", 10, "Diamonds"));
deck.add(new Card("Ace", 1, "Clubs"));
deck.add(new Card("2", 2, "Clubs"));
deck.add(new Card("3", 3, "Clubs"));
deck.add(new Card("4", 4, "Clubs"));
deck.add(new Card("5", 5, "Clubs"));
deck.add(new Card("6", 6, "Clubs"));
deck.add(new Card("7", 7, "Clubs"));
deck.add(new Card("8", 8, "Clubs"));
deck.add(new Card("9", 9, "Clubs"));
deck.add(new Card("10", 10, "Clubs"));
deck.add(new Card("Jack", 10, "Clubs"));
deck.add(new Card("Queen", 10, "Clubs"));
deck.add(new Card("King", 10, "Clubs"));
}
}

public void shuffle() {
ArrayList<Card> temp = new ArrayList<Card>();
while (!deck.isEmpty()) {
int loc = (int) (Math.random() * deck.size());
temp.add(deck.get(loc));
deck.remove(loc);
}
deck = temp;
}

public Card getDeck() {
return deck;
}

public Sting getCardName(int arrayIndex) {
return deck(arrayIndex, Name);
}

public int getCardValue(int arrayIndex) {
return deck(arrayIndex, Value);
}

public String getCardSuit(arrayIndex, Suit);
{
return deck(arrayIndex, Suit);
}
}

尝试运行:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
deck cannot be resolved

at Blackjack.<init>(Blackjack.java:61)
at Blackjack.main(Blackjack.java:72)

感谢您提前提供的所有帮助!

最佳答案

问题很多...

  • 您正在尝试运行无法编译的代码——切勿这样做。在尝试运行程序之前先解决编译问题。
  • 您正尝试调用类中尚未声明的变量的方法。 Blackjack 需要一个牌组变量,如果您要调用它的方法,该变量就会被初始化。
  • 我会考虑创建一个 Deck 类来保存 ArrayList 并具有一副牌的所有行为(方法)。
  • Card 类不应该有套牌字段。从逻辑上讲,为什么一张卡牌需要知道它所在的牌组。
  • 卡牌绝对不应该延长二十一点
  • 相反,Card 应该封装单张卡牌的概念,即它的花色和等级,仅此而已。
  • 如果需要,稍后您可以使用 HashMap 将每个卡片与图像或 ImageIcon 相关联。
  • Card/Deck 概念是教授枚举时使用的规范示例之一,因此借用这个想法 - 将您的 Suit 和 Rank 放入枚举中,并让您的 Card 类保存每个枚举的字段。<
<小时/>

关于,

What do you mean by trying to run uncompilable code?

每当你看到这个:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
deck cannot be resolved

这恰恰意味着:您正在尝试运行无法编译的代码。您必须在编译阶段修复此问题,而不是在运行阶段。

I mean, without the deck.shuffle(); , it does compile and runs okay.

那么您必须在重新尝试运行代码之前修复 deck.shuffle() 错误。

Is it that the way this code is structured, trying to accomplish my goals will be impossible?

我会从头开始,使用更干净的类,尤其是 Card 类,它是一切的关键。像我建议的那样使用枚举,在这里阅读它们:Java enums tutorial .

"Blackjack needs a deck variable that is initialized if you're ging to call a method on it.", but I thought I could just use the Deck variable in the other class?

您只能对调用代码范围内的字段调用方法。这是一个基本的 Java 101 概念,因此您最好回顾一下如何调用类上的方法。您的 Blackjack 类没有牌组字段,它根本不存在于该类中,因此您无法对不存在的字段或变量调用方法。

Can't I reference arrays, objects, variables, static things, whatever you want etc from other classes one way or another? Sorry for my lack of understanding, I'm still very much a leaner.

这些是超出本站点范围的基本问题,建议您重新查看一本像样的 Java 书籍或教程的介绍性章节,从中受益。相信我,你不会后悔这样做的。

关于java - 在 Java 中从另一个类调用 ArrayList 和方法很困难,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29288843/

25 4 0