User input to repeat program in Java

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

    one issue :

    Do not compare the content of two strings by == which just you should use equals()

    Imagine the right answer is one in this case

    Follow this as your blue print sample

      int answer = 0;
      String yes = "";
      Scanner input = new Scanner(System.in);
      do{
        do{
            System.out.println("Enter your number");
            answer = input.nextInt();
      } while ( answer != 1);
          System.out.println("Would you like to play again?");
          yes = input.next();
      } while ( yes.equalsIgnoreCase("yes"));
    

    output:

    enter image description here

提交回复
热议问题