How to repeat the if statement when the output is false

I am working on a simple game in which the user must guess a random number. I have all the code, except for the fact that if the hunch is too high or too low, I don’t know how to let them re-enter the number and keep playing until they get it. He just stops; here is the code:

import java.util.Scanner; import java.util.Random; public class Test { public static void main(String[] args) { Scanner input = new Scanner(System.in); Random rand = new Random(); int random = rand.nextInt(10) + 1; System.out.print("Pick a number 1-10: "); int number = input.nextInt(); if (number == random) { System.out.println("Good!"); } else if (number > random) { System.out.println("Too Big"); } else if (number < random) { System.out.println("Too Small"); } } } 
+5
source share
7 answers

To repeat everything you need a loop.

The usual way to repeat until the condition in the middle of the loop body is satisfied is to build an infinite loop and add a way to exit it.

The idiomatic way to create an infinite loop in Java is while(true) :

 while (true) { System.out.print("Pick a number 1-10: "); int number = input.nextInt(); if (number == random) { System.out.println("Good!"); break; // This ends the loop } else if (number > random) { System.out.println("Too Big"); } else if (number < random) { System.out.println("Too Small"); } } 

This loop will continue its iterations until the code path reaches the break statement.

+7
source

For this you need to use loop . The code should work as follows:

 import java.util.Scanner; import java.util.Random; public class Test { public static void main(String[] args) { Scanner input = new Scanner(System.in); Random rand = new Random(); int random = rand.nextInt(10) + 1; System.out.print("Pick a number 1-10: "); int number = input.nextInt(); boolean found = false; while (!found) { if (number == random) { System.out.println("Good!"); found = true; } else if (number > random) { System.out.println("Too Big, try again:"); number = input.nextInt(); } else if (number < random) { System.out.println("Too Small, try again:"); number = input.nextInt(); } } } } 
+2
source

There are several methods for looping around your query:

  • while (<condition>) { <do something> }

  • do { <something> } while (<condition>);

  • for (<init statement>, <condition>, <update statement>) { <do something> }

To demonstrate, you can avoid using one of the above explicit loop constructs using recursion:

 mport java.util.Scanner; import java.util.Random; public class Test { public static void ask(int random) { Scanner input = new Scanner(System.in); System.out.print("Pick a number 1-10: "); int number = input.nextInt(); if (number == random) { System.out.println("Good!"); } else if (number > random) { System.out.println("Too Big"); ask(random); } else if (number < random) { System.out.println("Too Small"); ask(random); } } public static void main(String[] args) { Random rand = new Random(); int random = rand.nextInt(10) + 1; ask(random); } } 

Here, the ask() method continues to call itself until the final condition is reached (user guessed).

Depending on the skill of the Java virtual machine, this may affect the call stack or not.

+1
source

You can use do...while .

 Random rand = new Random(); int random = rand.nextInt(10) + 1; do { Scanner input = new Scanner(System.in); System.out.print("Pick a number 1-10: "); int number = input.nextInt(); if (number == random) { System.out.println("Good!"); } else if (number > random) { System.out.println("Too Big"); } else if (number < random) { System.out.println("Too Small"); } } while ( number != random ); 
0
source

Close the if statements inside the do-while loop, which will cycle through until the user guesses the number:

  int number; do { System.out.print("Pick a number 1-10: "); number = input.nextInt(); if (number == random) { System.out.println("Good!"); } else if (number > random) { System.out.println("Too Big"); } else if (number < random) { System.out.println("Too Small"); } } while (number != random); 
0
source

To repeat the code conditionally, use a loop.

 // this code will execute only once System.out.print("Pick a number 1-10: "); // initialize number to a value that would not be used and not equal random int number = -1; // the code inside the curly braces will repeat until number == random while (number != random) { // get next number number = input.nextInt(); // handle case one if(number > random) System.out.println("Too Big"); // handle case two if(number < random) System.out.println("Too Small"); } // once number == random, the condition is false so we break out of the loop System.out.println("Good!"); 
0
source

What you are looking for are programming language constructs that allow you to do something specific time and time again.

This is done using loops. For example, docs for a while loop . That's what you need.

-1
source

Source: https://habr.com/ru/post/1232231/


All Articles