使用用户输入填充ArrayList

时间:2015-10-14 00:57:06

标签: java arraylist

我确信这已被问过1000次,但我无法想象我的生活。我搜索了堆栈溢出,谷歌,代码牧场等,但找不到我要找的东西。我需要能够获取一个数字,并在生成时将其添加到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

1 个答案:

答案 0 :(得分:0)

您使用List的唯一地方就是:

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);
}

在循环的每次迭代中,您将添加完全相同的值,即guesses引用的整数。例如,如果花了5次猜测,你将把数字 5 添加到数组中5次。

每次退出test时,您都会取消引用guessNumber(),因此为了将其保留在范围内,您需要在那里执行一些有用的操作,或者具有类级别(“ field“)变量引用ArrayList

另外,我注意到你不必要地两次打电话给generateRandomNumber()

//Calls the generateRandomNumber method to pick a random number to guess
generateRandomNumber();
//Assigns the generateRandomNumber method to the integer randomNumber
randomNumber = generateRandomNumber();

第一次浪费 - 其返回值未使用。