Skip to content

Commit 4452422

Browse files
Create NumberGuess.java
1 parent dbe49e1 commit 4452422

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

games/NumberGuess.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import java.util.Random;
2+
import java.util.Scanner;
3+
4+
public class Main{
5+
public static void main(String[] args){
6+
7+
// NUMBER GUESSING GAME
8+
9+
Random random = new Random();
10+
Scanner scanner = new Scanner(System.in);
11+
12+
// Declare variables
13+
int guess;
14+
int attempts = 0;
15+
int min = 1;
16+
int max = 100;
17+
int randomNumber = random.nextInt(min, max + 1);
18+
19+
/*
20+
Guess the number in a certain range (min-max range). If the guess is right, you win. Else, try again!
21+
*/
22+
System.out.println("..........NUMBER GUESSING GAME..........");
23+
System.out.printf("Guess a number between %d-%d:\n ", min, max);
24+
25+
do{
26+
System.out.print("Enter a guess: ");
27+
guess = scanner.nextInt();
28+
attempts++;
29+
30+
if(guess < randomNumber){
31+
System.out.println("TOO LOW! Try again!");
32+
}
33+
else if(guess > randomNumber){
34+
System.out.println("TOO HIGH! Try again!");
35+
}
36+
else{
37+
System.out.println("CORRECT! The number was " + randomNumber);
38+
System.out.println("Number of attempts: " + attempts);
39+
}
40+
41+
}while(guess != randomNumber);
42+
43+
System.out.println("You have won!");
44+
45+
scanner.close();
46+
47+
}
48+
}

0 commit comments

Comments
 (0)