Java number guessing game (standard programming problem)

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

Java number guessing game (standard programming problem)
[Personal memo] Number guessing game
Age guessing game made in Java
java (random number)
java programming basics
Java standard logger
[Java] Problem No. 2
[Java] Problem No.3
java Generic Programming
[Java] Problem No.1
I made a simple calculation problem game in Java
Constraint programming in Java
java standard functional interface
Rock-paper-scissors game java practice
Java programming basics practice-array
Java programming (class method)
Java programming (class structure)
All about Java programming
java competitive programming memo
[java] Decimal number → n-ary number n-ary number → decimal number
Java Programming Thread Runnable