これはおそらく非常に単純な質問ですが、私の人生では、何が問題なのかを理解することはできません. 私はこれを使ってJavaを独学し始めました: http://math.hws.edu/javanotes/
参考までに、このプログラムは「TextIO」と呼ばれる教科書の入出力クラスに依存しています。コードはhttp://math.hws.edu/javanotes/source/TextIO.javaにあります。
私は非常に単純なブラックジャック プログラムを作成し、勝ったゲーム数と合計金額の両方について、ハイ スコアを追跡することにしました。プログラムは、テキスト ファイルからスコアを読み取り、ユーザーがより高いスコアを取得したときに、そのテキスト ファイルに情報を書き直すことになっています。
とにかく、ほとんどのゲームまたはほとんどのお金を獲得するためにあなたの名前を入力できるようにしたいことを除いて、すべてうまくいくようです. 私はそれを非常に単純化して、(実際にハイスコアを取得したかどうかを尋ねるだけでなく) 問題が何であるかを確認するために名前を 2 回尋ねるだけで、両方とも「名前を入力してください」という質問を出力するようにしました。回ですが、入力は一度しか読み取れません。奇妙なことは、最初の入力のみを読み取ることです! コードは次のとおりです。
/* This program allows the user to play a game of
enter code here`* blackjack against the computer. The user can bet money
* the game will keep track of total winnings as well as
* the number of games won.
*/
public class BlackjackGame {
//keep track of highest scoring players so far
static String nameForGames; //name of person who won most games
static String nameForWinnings; //name of person who won most money
static int maxGames; //highest # of games won
static int maxWinnings; //highest amount of money earned
static int gamesWon = 0; //keeps track of total games won by user
static int money = 100; //total pot of money available for betting
private static Deck deck = new Deck();
static boolean wantToPlay; //answers whether the user wants to keep playing
public static void main(String[] args) {
//load the high score data from the scores.txt file
TextIO.readFile("scores.txt");
nameForGames = TextIO.getlnString();
maxGames = TextIO.getlnInt();
nameForWinnings = TextIO.getlnString();
maxWinnings = TextIO.getlnInt();
TextIO.putln(nameForGames + " " + maxGames + " " + nameForWinnings + " " + maxWinnings);
TextIO.readStandardInput();
introduction();
TextIO.put("Would you like to play");
wantToPlay = TextIO.getBoolean();
//user can keep playing indefinitely until they decide to stop
while (wantToPlay == true) {
playGame();
if (wantToPlay == false) {
break;
}
TextIO.put("Play again?");
wantToPlay = TextIO.getBoolean();
}
//print results of gameplay
TextIO.putln("\nThanks for playing!");
TextIO.putln("You won " + gamesWon + " games and walk away with $" + money + ".");
TextIO.putln();
//THIS IS THE PART THAT DOESNT MAKE SENSE? WHY IS IT ONLY READING INPUT ONCE?
TextIO.put("Enter your name: ");
nameForWinnings = TextIO.getln();
TextIO.putln();
TextIO.put("Enter your name: ");
nameForGames = TextIO.getln();
//Save new high scores to scores.txt.
TextIO.writeFile("scores.txt");
TextIO.putln(nameForGames);
TextIO.putln(maxGames);
TextIO.putln(nameForWinnings);
TextIO.putln(maxWinnings);
//Re-read the data from the file, and display to the player.
System.out.println("The high scores are:");
TextIO.readFile("scores.txt");
nameForGames = TextIO.getlnString();
maxGames = TextIO.getlnInt();
nameForWinnings = TextIO.getlnString();
maxWinnings = TextIO.getlnInt();
TextIO.writeStandardOutput();
TextIO.putln(nameForGames + " won " + maxGames + " games!");
TextIO.putln(nameForWinnings + " won $" + maxWinnings + " total cash!");
} //end main()