Task 1
Task 1
Number Guessing Game: Create a simple number guessing game where the program
generates a random number and the user tries to guess it.
PROGRAM
import java.util.Random;
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Random random = new Random();
Scanner scanner = new Scanner(System.in);
int num = random.nextInt(100) + 1;
int guess = -1;
int count = 0;
System.out.println("Number Guessing Game");
System.out.println("✮-✮-✮-✮-✮-✮-✮-✮-✮");
System.out.print("Guess the number (between 1 and 100): ");
while (guess != num) {
guess = scanner.nextInt();
count++;
if (guess > num) {
System.out.println("Lower than this");
} else if (guess < num) {
System.out.println("Greater than this");
} else {
System.out.println("Finally......! You got it");
System.out.println("You guessed it in " + count + " attempts.");
}
if (guess != num) {
System.out.print("Guess again: ");
}
}
scanner.close();
}
}
OUTPUT