第一次海报,长期潜伏者。
我是第二年计算机科学专业的学生试图编写一个二十一点游戏。它不是一个家庭作业,它只是一个有趣的项目。它还没有完成游戏本身没有准备好,所以请忽略该部分。我花了大约两个小时搜索SO以获得答案,但无法与其他用户进行比较。我自己也有类似的问题,所以我决定发布我的问题。
我的问题是' deck'数组列表。从我的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张卡片,但为了简单起见,我决定不这样做)。从我读到的内容来看,如果我不想制作静态方法,我必须做出“吸气”的方法。方法。我不太确定返回方法如何帮助我,比如说,从二十一点类中洗牌,或者从二十一点类中抽取牌(尚未实现),因为这些方法因返回类型而无效。我甚至不确定我的吸气方法是否正确,因为我从来没有尝试过这样做。之前构造函数所需的参数。
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)
提前感谢您的所有帮助!
答案 0 :(得分:7)
很多问题......
关于,
尝试运行无法编译的代码是什么意思?
每当你看到这个:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
deck cannot be resolved
这恰恰意味着:您正在尝试运行无法编译的代码。您必须在编译阶段而不是在运行阶段修复此问题。
我的意思是,没有deck.shuffle(); ,它确实编译并运行正常。
然后,您必须在重新尝试运行代码之前修复deck.shuffle()
错误。
这个代码的结构方式,试图实现我的目标是不可能的吗?
我会从一开始就重新开始使用更干净的课程,特别是你的Card课程,这是一切的关键。使用我建议的枚举,请在此处阅读:Java enums tutorial。
“二十一点需要一个已初始化的牌组变量,如果你想在它上面调用一个方法。”但是我想我可以在另一个类中使用Deck变量?
您只能在调用代码范围内的字段上调用方法。这是一个基本的Java 101概念,因此您最好查看如何在类上调用方法。你的Blackjack类没有deck字段,它在该类中根本不存在,因此你不能在不存在的字段或变量上调用方法。
我不能以某种方式从其他类引用数组,对象,变量,静态事物,任何你想要的等等吗?抱歉,由于我缺乏理解,我仍然非常精简。
这些是超出本网站范围的基本问题,并建议您从重新审阅一本体面的Java书籍或教程的介绍性章节中受益。相信我,你不会后悔这样做。