Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么我的Java程序只读取一行输入?_Java - Fatal编程技术网

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

为什么我的Java程序只读取一行输入?,java,Java,这可能是一个非常简单的问题,但我一辈子都搞不清楚到底出了什么问题。我刚开始用以下方法自学Java: 作为参考,该程序依赖于教科书中名为“TextIO”的输入/输出类。您可以在此处找到它的代码: 我制作了一个超级简单的21点程序,并决定尝试跟踪高分:赢得的游戏数和总金额。程序应该从文本文件中读取分数,然后在用户获得更高分数时在该文本文件中重新写入信息 无论如何,一切似乎都很好,除了我希望你能够输入你的名字,赢得大多数比赛或大多数钱。我对它进行了超级简化,让它只询问你的名字两次,看看问题是什么(而不

这可能是一个非常简单的问题,但我一辈子都搞不清楚到底出了什么问题。我刚开始用以下方法自学Java:

作为参考,该程序依赖于教科书中名为“TextIO”的输入/输出类。您可以在此处找到它的代码:

我制作了一个超级简单的21点程序,并决定尝试跟踪高分:赢得的游戏数和总金额。程序应该从文本文件中读取分数,然后在用户获得更高分数时在该文本文件中重新写入信息

无论如何,一切似乎都很好,除了我希望你能够输入你的名字,赢得大多数比赛或大多数钱。我对它进行了超级简化,让它只询问你的名字两次,看看问题是什么(而不是只询问你是否真的得了高分),问题是它两次都打印“输入你的名字”,但只读取一次输入,奇怪的是它只读取第一个输入!代码如下:

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

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

您是说
nameForWinnings
nameForGames
在打印时打印相同的值吗?即使您输入了不同的名称?在您的代码中,您有这个“/”这是没有意义的部分?为什么它只读取一次输入?”我测试了该部分,它读取了两次输入。您可以将nameForWinnings复制到nameForGames,而不是从同一个用户处读取两次。对不起,我这里没有回答确切的问题。您是说
nameForWinnings
nameForGames
在打印时打印相同的值吗?即使您输入了不同的名称?在您的代码中,您有这个“/”这是没有意义的部分?为什么它只读取一次输入?”我测试了该部分,它读取了两次输入。您可以将nameForWinnings复制到nameForGames,而不是从同一个用户处读取两次。对不起,我没有回答确切的问题。