难以从 Java 中的另一个 Class 调用 ArrayLists 和方法
Difficulty calling ArrayLists and Methods from another Class in Java
第一次 post呃,长期潜伏者。
我是计算机科学专业的二年级学生,正在尝试编写二十一点游戏代码。这不是家庭作业,它只是一个有趣的项目。这还没有完成,所以游戏本身还没有准备好,所以请忽略该部分。我花了大约两个小时在 SO 上搜索答案,但无法将其他用户的类似问题与我自己的问题进行比较,所以我决定 post 我的问题。
我的问题是 'deck' ArrayList。从我的 Blackjack.java 文件中,无论我做什么,我都无法调用它,我无法调用我的 Card class 中的任何方法,我无法操作它,我似乎无法在没有收到一些东西的情况下做任何事情某种错误。
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 ): $");
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 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 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();
}
}
还有卡片 class(我知道我可以循环完成 52 张卡片,但为了简单起见,我决定不这样做)。根据我的阅读,如果我不想制作静态方法,我必须制作 'getter' 方法。我不太确定 return 方法将如何帮助我,例如,从 blackjack class 洗牌,或从 blackjack class 抽牌(尚未实现),因为这些方法的 return 类型是 void 。我什至不确定我的 getter 方法是否正确,因为我以前从未尝试过 'get' 构造函数所需的参数。
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)
提前感谢大家的帮助!
很多问题...
- 您正在尝试 运行 不可编译的代码 -- 永远不要这样做。在尝试 运行 程序之前先解决编译问题。
- 您正在尝试对尚未在 class 中声明的变量调用方法。 Blackjack 需要一个 deck 变量,如果您要在其上调用方法,则该变量已初始化。
- 我会考虑创建一个 Deck class,它包含 ArrayList 并且具有一副纸牌的所有行为(方法)。
- 卡片 class 不应有牌组区域。从逻辑上讲,为什么卡片需要知道它所在的牌组。
- Card 绝不能延长 Blackjack
- 相反,Card 应该封装单张 Card 的概念,意思是它的花色和等级,没有别的。
- 稍后您可以根据需要使用 HashMap 将每张卡片与图像或 ImageIcon 相关联。
- Card/Deck 概念是教授枚举时使用的典型示例之一,所以借用这个想法——将你的 Suit 和 Rank 放入枚举中,并让你的 Card class 持有一个字段这些枚举中的每一个。
关于,
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?
我会从头开始使用更清洁的 classes,尤其是您的卡 class,这是一切的关键。像我建议的那样使用枚举,在这里阅读它们: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 概念,因此您最好回顾一下如何调用 classes 上的方法。您的 Blackjack class 没有甲板区域,它根本不存在于 class 中,因此您不能在不存在的区域或变量上调用方法。
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 书籍或教程的介绍性章节,这会对您有所帮助。相信我,你不会后悔的。
第一次 post呃,长期潜伏者。
我是计算机科学专业的二年级学生,正在尝试编写二十一点游戏代码。这不是家庭作业,它只是一个有趣的项目。这还没有完成,所以游戏本身还没有准备好,所以请忽略该部分。我花了大约两个小时在 SO 上搜索答案,但无法将其他用户的类似问题与我自己的问题进行比较,所以我决定 post 我的问题。
我的问题是 'deck' ArrayList。从我的 Blackjack.java 文件中,无论我做什么,我都无法调用它,我无法调用我的 Card class 中的任何方法,我无法操作它,我似乎无法在没有收到一些东西的情况下做任何事情某种错误。
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 ): $");
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 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 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();
}
}
还有卡片 class(我知道我可以循环完成 52 张卡片,但为了简单起见,我决定不这样做)。根据我的阅读,如果我不想制作静态方法,我必须制作 'getter' 方法。我不太确定 return 方法将如何帮助我,例如,从 blackjack class 洗牌,或从 blackjack class 抽牌(尚未实现),因为这些方法的 return 类型是 void 。我什至不确定我的 getter 方法是否正确,因为我以前从未尝试过 'get' 构造函数所需的参数。
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)
提前感谢大家的帮助!
很多问题...
- 您正在尝试 运行 不可编译的代码 -- 永远不要这样做。在尝试 运行 程序之前先解决编译问题。
- 您正在尝试对尚未在 class 中声明的变量调用方法。 Blackjack 需要一个 deck 变量,如果您要在其上调用方法,则该变量已初始化。
- 我会考虑创建一个 Deck class,它包含 ArrayList 并且具有一副纸牌的所有行为(方法)。
- 卡片 class 不应有牌组区域。从逻辑上讲,为什么卡片需要知道它所在的牌组。
- Card 绝不能延长 Blackjack
- 相反,Card 应该封装单张 Card 的概念,意思是它的花色和等级,没有别的。
- 稍后您可以根据需要使用 HashMap 将每张卡片与图像或 ImageIcon 相关联。
- Card/Deck 概念是教授枚举时使用的典型示例之一,所以借用这个想法——将你的 Suit 和 Rank 放入枚举中,并让你的 Card class 持有一个字段这些枚举中的每一个。
关于,
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?
我会从头开始使用更清洁的 classes,尤其是您的卡 class,这是一切的关键。像我建议的那样使用枚举,在这里阅读它们: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 概念,因此您最好回顾一下如何调用 classes 上的方法。您的 Blackjack class 没有甲板区域,它根本不存在于 class 中,因此您不能在不存在的区域或变量上调用方法。
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 书籍或教程的介绍性章节,这会对您有所帮助。相信我,你不会后悔的。