I made a classic game. Generate random numbers up to 1000 and guess the number. If you do not hit within 7 times, the game is over. For me, I think there is only one thing that is rare.
It was dangerous that I had forgotten the work of break.
Generate a random number and guess the number. If you can't guess within 7 times, the game is over.
import java.util.Random;
import java.util.Scanner;
public class NumberQuiz {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
Random random = new Random();
int randomNumber = random.nextInt(1001);
System.out.println("I made a number guessing game. Enter any number from 0 to 1000. Please hit within 7 times");
int number = scan.nextInt();
int i;
final int LIMIT = 8;
for (i = 0; i<=LIMIT ; i++) {
int th = i + 2;
if (th == LIMIT) {
System.out.println(LIMIT + "It was the second time. The game is over. The correct answer is" + randomNumber + "was.");
break;
}else {
if (number > randomNumber) {
System.out.print("It's smaller.");
System.out.println("Please enter any number again." + th + "This is the second challenge");
number = scan.nextInt();
}else if (number < randomNumber) {
System.out.print("It's bigger.");
System.out.println("Please enter any number again." + th + "This is the second challenge");
number = scan.nextInt();
}else if (number == randomNumber) {
System.out.println("Is the correct answer." + (i+2) + "I arrived at the time!");
break;
}
}
}
scan.close();
}
}
Recommended Posts