0% found this document useful (0 votes)
137 views2 pages

05 Hands-On Activity 1

This Java code implements a number guessing game that randomly generates a secret number between 1 and 1000. It prompts the user to enter a guess and uses conditionals to compare the guess to the secret number, printing feedback on whether the guess is too high, too low, or correct. The code repeats in a loop until the user guesses correctly.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
137 views2 pages

05 Hands-On Activity 1

This Java code implements a number guessing game that randomly generates a secret number between 1 and 1000. It prompts the user to enter a guess and uses conditionals to compare the guess to the secret number, printing feedback on whether the guess is too high, too low, or correct. The code repeats in a loop until the user guesses correctly.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

import java.util.

Scanner;

public class NumberGuessingGame {

public static void main(String[] args) {

int secretNumber;

secretNumber = (int) (Math.random() * 999 + 1);

Scanner keyboard = new Scanner(System.in);

int guess;

do {

System.out.print("Enter a guess (1-1000): ");

guess = keyboard.nextInt();

if (guess == secretNumber)

System.out.println("Your guess is correct. Congratulations!");

else if (guess < secretNumber)

System.out

.println("Your guess is smaller than the secret number.");

else if (guess > secretNumber)

System.out

.println("Your guess is greater than the secret number.");

} while (guess != secretNumber);


}

You might also like