简体   繁体   English

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

[英]Why is my Java program only reading one line of input?

this is probably a really simple question, but I can't for the life of me figure out what's going wrong. 这可能是一个非常简单的问题,但是我无法终生发现问题所在。 I just started teaching myself Java using this: http://math.hws.edu/javanotes/ 我刚刚开始使用以下方法自学Java: http : //math.hws.edu/javanotes/

For reference, this program relies on an input/output class from that textbook called "TextIO". 作为参考,该程序依赖于该教科书中称为“ TextIO”的输入/输出类。 You can find the code for it here: http://math.hws.edu/javanotes/source/TextIO.java . 您可以在这里找到它的代码: http : //math.hws.edu/javanotes/source/TextIO.java

I made a super simple blackjack program, and I decided to try to keep track of high scores: both for # of games won, and total amount of money. 我制作了一个超级简单的二十一点程序,我决定尝试跟踪高分:获胜游戏数和总金额。 The program is supposed to read scores from a text file, and then re-write the info in that text file when the user gets a higher score. 该程序应该从文本文件中读取分数,然后在用户获得较高分数时将信息重新写入该文本文件中。

Anyway, everything seems to work fine, except that I want you to be able to enter your name for winning either most games or most money. 无论如何,一切似乎都正常,除了我希望您能够输入赢得大多数游戏或最多钱的名字。 I super-simplified it so that it just asks your name twice to try to see what the problem was (rather than only asking if you actually get a high score), and the thing is that it prints the question "enter your name" both times, but only reads input once, and the weird thing is that it only reads the input for the first one! 我对其进行了超级简化,以便它仅询问您的名字两次以尝试查看问题所在(而不是仅询问您是否获得高分),而事实是,它会同时打印“输入您的名字”问题次,但只读取一次输入,而奇怪的是它只读取第一个输入! Here's the code: 这是代码:

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

It seems that the getBoolean method that you call before reading the user's name does not remove line separator characters from the stream. 似乎在读取用户名之前调用的getBoolean方法不会从流中删除行分隔符。 This means that the first call to readln will return what ever is left of the line, even if it's an empty string. 这意味着对readln的第一次调用将返回该行的剩余内容,即使它是一个空字符串也是如此。 Looking at the methods that are available to you you probably should use getlnBoolean instead. 查看可用的方法,您可能应该改用getlnBoolean

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM