Created
August 4, 2021 00:30
-
-
Save 4anonz/1dcb18213ec2a637c3f20e9a79e16750 to your computer and use it in GitHub Desktop.
Java Number Guessing Game With Random Number Generator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Beginner Simple Number Guessing Game using random number generator | |
import java.util.Random; | |
import java.util.Scanner; | |
public class GuessingGame { | |
public static void main(String[] args) { | |
Random random = new Random(); | |
Scanner inputs = new Scanner(System.in); | |
int secretNumber = 1 + random.nextInt(9); | |
System.out.println(">>> Number Guessing Game <<<"); | |
System.out.println("You have only 3 tries to get the correct number"); | |
System.out.println("The secret number is between 1 and 9, Good luck!"); | |
boolean isSuccess = false; | |
int count = 0, userGuess; | |
do { | |
count++; | |
System.out.print("Guess$> "); | |
userGuess = inputs.nextInt(); | |
if(userGuess == secretNumber) { | |
isSuccess = true; | |
break; | |
} | |
}while(count < 3); | |
if(isSuccess) | |
System.out.println("You Won! (^.^)"); | |
else { | |
System.out.println("Game Over! :("); | |
System.out.printf("Secret Number is %d\n", secretNumber); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Cool