Java 使用用户输入填充ArrayList

Java 使用用户输入填充ArrayList,java,arraylist,Java,Arraylist,我相信这个问题已经被问过1000次了,但我无法用我的生命来证明这一点。我已经搜索了stack overflow、google、code ranch等,但找不到我要找的内容。我需要能够获取一个数字,并在生成时将其添加到ArrayList中。程序生成一个随机数,然后让用户尝试猜测它是什么。我可以获得正确填充的猜测数,但无法将其存储在ArrayList中。我需要每个猜测都在一个新的索引中,我不知道由于用户决定玩多少游戏,我需要多少。我甚至不确定ArrayList是否是我需要使用的。存储它们的意义在于,

我相信这个问题已经被问过1000次了,但我无法用我的生命来证明这一点。我已经搜索了stack overflow、google、code ranch等,但找不到我要找的内容。我需要能够获取一个数字,并在生成时将其添加到ArrayList中。程序生成一个随机数,然后让用户尝试猜测它是什么。我可以获得正确填充的猜测数,但无法将其存储在ArrayList中。我需要每个猜测都在一个新的索引中,我不知道由于用户决定玩多少游戏,我需要多少。我甚至不确定ArrayList是否是我需要使用的。存储它们的意义在于,我可以在程序的后面找到最大数量的猜测。下面是我猜测数字并向用户显示的代码。我使用猜测作为int来存储来自用户猜测的信息。我是JAVA新手,所以请原谅所有的评论,让我跟上进度

    public static void guessNumber() {

    //Totals the games played by the user
    gamesPlayed++;
    //Scanner to allow user input
    Scanner console = new Scanner(System.in);
    //Prints "Your guess ? " for the user to input their guess
    System.out.print("Your guess ? ");
    // Input for the number the user guessed
    int numberGuessed = console.nextInt();

    //While loop with nested if statement to check if the number guessed by the user is equal to the random number generated, if not,
    //show higher or lower hints depending on the guess
    while (numberGuessed != randomNumber) {

        //if statement to determine if the guess is higher or lower than the random number
        //if the guess is higher, show the random number is lower than the guess, than ask the user to guess again
        if (numberGuessed > randomNumber) {
            //Prints "lower" for the user to know the number is lower than the guess, 
            //Prints "Your guess ? " for the user to guess again
            System.out.println("lower");
            System.out.print("Your guess ? ");
            //End of if statement and begins else if statement to determine if the guess is higher or lower than the random number
            //If the guess is lower, show the random number is higher than the guess, 
            //Prints "Your guess ? " for the user to guess again
        } else if (numberGuessed < randomNumber) {
            //Prints "higher" for the user to know the number is higher than the guess, 
            //"Your guess ? " for the user to guess again
            System.out.println("higher");
            System.out.print("Your guess ? ");
        }// End of else if statement

        // Input for the number the user guessed
        numberGuessed = console.nextInt();
        //Counts the number of tries it took to guess the correct number
        guesses++;
        //Totals all the guesses made in the games played
        totalGuesses++;

    }//End of while loop

    //Array for storing guesses
    System.out.println("");      
    ArrayList<Integer> test = new ArrayList<Integer>();
    for (int i = 0; guesses > i; i++) {
        test.add(guesses);
    System.out.println("Elements of ArrayList of Integer Type: "+ test);
    }

    //Shows the user guessed the right number and how many tries it took to guess the number
    System.out.println("You got it right in " + guesses + " guesses" + "\n");
    //Ask the user if they want to play again
    System.out.print("Do you want to play again? ");
    //Input for the user to input yes or no if they want to play again or not
    String playAgain = console.next();
    //Prints blank line
    System.out.println("");
    //Totals all the guesses made in the game
    double endingTotalGuesses = totalGuesses + gamesPlayed;

    //If statement to check and see if the user wants to play again and ignores the case of the word no
    if (playAgain.equalsIgnoreCase("yes")) {
        //Resets the number of guesses when a used starts a new game
        guesses = 1;
        //Prints I'm thinking of a number...
        System.out.println("I'm thinking of a number...");
        //Calls the generateRandomNumber method to pick a random number to guess
        generateRandomNumber();
        //Assigns the generateRandomNumber method to the integer randomNumber
        randomNumber = generateRandomNumber();
        //*Remove this line that shows what the number is!*
        System.out.println("The number is: " + randomNumber);
        //Calls the guessNumber method to allow the game to start over and the user to start guessing
        guessNumber();
        //End of if statement and starts the else if statement to see if the user wants to play again and ignores the case of the word no
    } else if (playAgain.equalsIgnoreCase("no")) {
        //else if statement. If the user chooses no, it will call the endOfGames method and pass the 
        //ending amount of guesses and total of games played
        endOfGames(endingTotalGuesses, gamesPlayed);

    }//end of else if statement

}//End of guessNumber method
publicstaticvoidguessnumber(){
//合计用户玩的游戏
gamesPlayed++;
//允许用户输入的扫描仪
扫描仪控制台=新扫描仪(System.in);
//打印“您的猜测?”供用户输入他们的猜测
系统输出打印(“您的猜测?”);
//输入用户猜测的数字
int numberguesed=console.nextInt();
//使用嵌套的if语句循环,检查用户猜测的数字是否等于生成的随机数,如果不等于,
//根据猜测显示较高或较低的提示
while(numberguesed!=随机数){
//if语句来确定猜测是高于还是低于随机数
//如果猜测值较高,则显示随机数低于猜测值,然后要求用户再次猜测
如果(NumberGuesed>randomNumber){
//打印“lower”,让用户知道数字低于猜测值,
//打印“您的猜测?”供用户再次猜测
系统输出打印项次(“下”);
系统输出打印(“您的猜测?”);
//if语句结束,else if语句开始,以确定猜测值是否高于或低于随机数
//如果猜测值较低,则表明随机数高于猜测值,
//打印“您的猜测?”供用户再次猜测
}else if(numberguesedi;i++){
测试。添加(猜测);
System.out.println(“整型数组列表的元素:“+test”);
}
//显示用户猜测正确的数字以及猜测该数字的尝试次数
System.out.println(“您在“+guesss+”guesss“+”\n”中做对了);
//询问用户是否希望再次播放
System.out.print(“您想再次播放吗?”);
//输入,供用户输入是或否,如果他们想再次播放或不播放
String playreach=console.next();
//打印空白行
System.out.println(“”);
//游戏中所有猜测的总数
double EndingTotalGuesss=总猜测数+已显示的游戏数;
//If语句来检查用户是否想再次播放,并忽略单词no的大小写
如果(再次播放.equalsIgnoreCase(“是”)){
//重置旧游戏开始新游戏时的猜测次数
猜测=1;
//我在想一个数字。。。
System.out.println(“我在想一个数字…”);
//调用GeneratorDomainNumber方法来选择要猜测的随机数
GeneratorDomainNumber();
//将GeneratorDomainNumber方法指定给整数randomNumber
randomNumber=GeneratorAndomNumber();
//*删除显示数字的这一行*
System.out.println(“编号为:“+randomNumber”);
//调用guessNumber方法以允许游戏重新开始,并允许用户开始猜测
猜数();
//结束if语句并启动else if语句以查看用户是否希望再次播放,并忽略单词no的大小写
}else if(playreach.equalsIgnoreCase(“no”)){
//如果用户选择否,它将调用endOfGames方法并传递
//结束猜数和玩的游戏总数
endOfGames(endingtotalgues,gamesPlayed);
}//else-if语句结束
}//猜数结束法

您使用的
列表的唯一位置是:

System.out.println("");      
ArrayList<Integer> test = new ArrayList<Integer>();
for (int i = 0; guesses > i; i++) {
    test.add(guesses);
    System.out.println("Elements of ArrayList of Integer Type: "+ test);
}

第一次是浪费的——它的返回值是未使用的

“我无法将其存储在ArrayList”是什么意思?它不编译吗?当您执行
System.out.println(“整数类型的ArrayList的元素:“+test”)时,它是否编译并正确打印列表?它编译并运行,我在获取多个要写入数组的条目时遇到问题。我已经厌倦了int.Next()尝试让它移动到下一个位置,我已经做了一段时间,但也没有成功。我真的不知道我是否需要这个循环。我需要它将数字写入数组,然后当用户再次猜测时,它将下一个值写入同一数组。我能解释的最好办法是,第一个猜测将保存在0位置,第二个猜测保存在1位置,依此类推。谢谢,o
//Calls the generateRandomNumber method to pick a random number to guess
generateRandomNumber();
//Assigns the generateRandomNumber method to the integer randomNumber
randomNumber = generateRandomNumber();