User input to repeat program in Java

后端 未结 8 1286
遇见更好的自我
遇见更好的自我 2020-12-11 11:41

I am writing a simple guessing game program where the user will input a number to try and guess a randomly generated number.

If they get the number right I want to

8条回答
  •  误落风尘
    2020-12-11 12:44

    Some of the solution I see above aren't correct. The random number, you need to add 1 to get between 1 and 10, also you need to compare with equals. I use case insensitive here.

    The following code works as you need it.

    import java.util.Random;
    import java.util.Scanner;
    
    public class Test2 {
        private static Random num = new Random();
        private static int answer = 0;
        private static int guess;
        private static String playAgain;
    
        public static void main(String[] args) {
            inputGuess();
        }
    
        // Guess Method.
        public static void inputGuess(){
            // create answer.
            answer = 1+ num.nextInt(10);
    
            System.out.println("Enter a number between 1 and 10 as your first guess: ");
            Scanner input = new Scanner(System.in);
            guess = input.nextInt(); 
            do{
            if (guess < 1 || guess > 10){
                System.out.println("That is not a valid entry. Please try again: ");
                guess = input.nextInt();
            }else if (guess > answer){
                System.out.println("Too high, Try Again: ");
                guess = input.nextInt();
            }else if (guess < answer){
                System.out.println("Too low, Try Again: ");
                guess = input.nextInt();
            }
    
            }while (guess != answer);
    
            System.out.println("Congratulations, You guessed the number!");
            System.out.println("Would you like to play again? Enter Y to play or any other key to quit: ");
            playAgain = input.nextLine();
            if( playAgain.equalsIgnoreCase("Y") ){ 
                inputGuess();
            }
    
        }
    }
    

提交回复
热议问题