如何修复此代码中的重播功能?

时间:2019-05-05 20:16:05

标签: java

我正在对此编码,以要求玩家猜测一个随机数。他们完成猜测或获得正确答案后,程序将结束。这部分暂时正常。但是,第二部分,当他们完成第一轮比赛时,我希望程序询问用户是否要重播。但是“我想重播”功能在这里不起作用。谁能帮忙修改它?

public static void main( String [] args )
{
    Scanner scan = new Scanner(System.in); //create scanner object

    int maxNumber = 0; //initial value for MaxNumber
    int userGuess = 0; //initial value for UserGuess
    int totalGuesses = 0; //initial value for TotalGuesses
    int randomNumber = 0; //initial value for RandomNumber
    int maxAttempts = 0; //initial value for MaxAttempts
    int end = 0;
    int replay = 0;

    do
    {        

            System.out.println( "Please enter a maximum value: " ); //Ask user to input a max number
            maxNumber = scan.nextInt(); //set MaxNumber to user input

            System.out.println( "Please enter a maximum attempts: " ); //Ask user to input max attempts
            maxAttempts = scan.nextInt(); //set Attempts to user input

            randomNumber = (int) ( maxNumber * Math.random() ) + 1; //generate a secret number
            System.out.println( "A new secret number has been generated" 
                + "\n Input your guess" ); //ask user to input a guess

        while (totalGuesses < maxAttempts)
        {
            userGuess = scan.nextInt(); //set UserGuess to user input 
    totalGuesses++; //increment total number of guesses by 1

            if (userGuess < randomNumber) //while user's guess is less than the secret number
            {
                System.out.println("Sorry, that is too low! Please try again.");  
            }

            else if (userGuess > randomNumber) //while user's guess is greater than the secret number
            {
                System.out.println("Sorry, that is too high! Please try again."); 
            }

            else if (userGuess == randomNumber) //if user guesses correctly
            {
                System.out.println(" Your guess is correct!" // let user know the guess is correct
                        + "\n Total number of guesses: " + totalGuesses); // display total number of guesses
            }

           while (userGuess == randomNumber | totalGuesses == maxAttempts)
           {
                System.out.println("Would you like to play again? Please input 1 for Yes or 2 for no and press enter."); 
                //ask user if they would like to play again
                replay = scan.nextInt();
                if (replay == 1);
                {
                 end == 0;
                }

                if (replay == 2); 
                {
                 end == 1;
                 System.out.print("Thanks for playing!");
                }
           }
        } 
    }while (end == 0);

}

我希望用户放1时可以重播。并且当他们输入2时程序应该停止。

0 个答案:

没有答案