User input to repeat program in Java

后端 未结 8 1261
遇见更好的自我
遇见更好的自我 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:33

    This code can serve your purpose...

    import java.util.Random;
    import java.util.Scanner;
    
    public class GuessingGame 
    {
    private Random num = new Random();
    private int answer ;
    private int guess;
    private String playAgain;
    
    public void inputGuess() 
    {         
        answer = num.nextInt(11);
        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: ");
        do
        {           
            playAgain = input.nextLine();
    
        }while(playAgain.length()<1);
    
        if (playAgain.trim().equalsIgnoreCase("y"))
        {
            inputGuess();           
        }
        else
        {
            System.out.println("Good Bye!!!");
        }
    
    }
    
    public static void main(String[] args) {
        new GuessingGame().inputGuess();
    }
       }
    
    0 讨论(0)
  • 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();
            }
    
        }
    }
    
    0 讨论(0)
提交回复
热议问题