私は単純なブラックジャック プログラムを書いていますが、Java が上達するにつれて拡張する予定です。私のコースでは継承についてあまり詳しく説明していませんでしたし、サブクラスについてもまったく議論していませんでした。
プレーヤーがゲームを終了することを決定したら、プレーヤーの合計チップ数を出力したいと考えています。printステートメントを書くたびに、エラーが発生します:
非静的フィールド alice への静的参照を作成できません。
static
私がよく知っているのはトップレベルのメソッドだけなので、非静的フィールドによってEclipseが何を意味するのかわかりません。参照を行うために名前を付けた完全に別のクラスを作成するPlayer
必要がありますか?
サブクラスとメイン メソッド全体に関連するコードのスニペットを添付しました。さらに、これは宿題ではなく、私のポートフォリオを構築するための個人的なプロジェクトなので、価値があると思われるその他のアドバイスを歓迎します. 助けてくれてありがとう。
public class BlackJack
{
public class Player extends BlackJack
{
public int wallet;
private int hand;
public Player(int playerHand, int playerWallet)
{
wallet = playerWallet;
hand = playerHand;
}
public int getWallet()
{ return wallet; }
}
Player alice = new Player(0, 250);
public static void main(String[] args)
{
System.out.println("Welcome to Black Jack. Type begin to get started.");
String begin = "begin";
String end = "end";
Scanner keyboard = new Scanner(System.in);
while (!keyboard.equals(end))
{
}
System.out.println("You walked away with $" + alice.getWallet() + ". Thanks for playing!");
}
}