gpt4 book ai didi

java - 为什么我的 Java 程序只读取一行输入?

转载 作者:太空宇宙 更新时间:2023-11-04 07:22:52 26 4
gpt4 key购买 nike

这可能是一个非常简单的问题,但我一生都无法弄清楚出了什么问题。我刚刚开始使用这个自学 Java:http://math.hws.edu/javanotes/

作为引用,该程序依赖于该教科书中名为“TextIO”的输入/输出类。您可以在这里找到它的代码:http://math.hws.edu/javanotes/source/TextIO.java .

我制作了一个 super 简单的二十一点程序,我决定尝试跟踪高分:赢得的游戏数量和总金额。该程序应该从文本文件中读取分数,然后当用户获得更高分数时将信息重写到该文本文件中。

无论如何,一切似乎都运行良好,除了我希望您能够输入您赢得最多游戏或最多金钱的名字。我对它进行了 super 简化,使它只询问你的名字两次,试图看看问题是什么(而不是只询问你是否真的得到了高分),问题是它两次打印问题“输入你的名字”,但只读取输入一次,奇怪的是它只读取第一次的输入!代码如下:

/* 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()

最佳答案

看来您在读取用户名之前调用的 getBoolean 方法不会从流中删除行分隔符。这意味着第一次调用 readln 将返回该行剩下的内容,即使它是一个空字符串。查看可用的方法,您可能应该使用 getlnBoolean 来代替。

关于java - 为什么我的 Java 程序只读取一行输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19039837/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com