Reputation: 333
I am working on a simple game in which the user has to guess a random number. I have all the code set up except for that fact that if the guess is too high or too low I don't know how to allow them to re-enter a number and keep playing until they get it. It 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");
}
}
}
Upvotes: 8
Views: 56426
Reputation: 11438
Enclose the if statements within a do-while loop, that will loop around while the user hasn't guessed 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);
Upvotes: 1
Reputation: 488
You need to use a loop for that. The code should work like this:
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();
}
}
}
}
Upvotes: 3
Reputation: 4414
In order to repeat 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!");
Upvotes: 0
Reputation: 5105
Several techniques exist to loop your request, among them:
while (<condition>) { <do something> }
do { <something> } while (<condition>);
for (<init statement>, <condition>, <update statement>) { <do something> }
To show off, you can avoid using one of the above explicit loop constructs by 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 keeps calling itself, until the end condition (user guessed right) is reached.
Depending on the cleverness of the Java virtual machine this might stress the call stack, or not.
Upvotes: 1
Reputation: 832
What you're looking for are constructs in the programming language that allow you do a specific thing again and again.
This is done using loops. Check the docs for the while loop for instance. That's what you need.
Upvotes: -1
Reputation: 726809
In order to repeat anything you need a loop.
A common way of repeating until a condition in the middle of loop's body is satisfied is building an infinite loop, and adding a way to break out of it.
Idiomatic way of making 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.
Upvotes: 11
Reputation: 5522
You could use a 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 );
Upvotes: 1