-
Notifications
You must be signed in to change notification settings - Fork 0
/
numberGame
57 lines (47 loc) · 1.84 KB
/
numberGame
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import java.util.Random;
import java.util.Scanner;
class numberGame {
public static void main(String[] args)
{
int min = 1;
int max = 100;
Random random = new Random();
int randomNumber = random.nextInt(max - min + 1) + min;
System.out.println("The generated random number is: " + randomNumber);
int yourGuess = random.nextInt(100)+1;
Scanner sc = new Scanner(System.in);
int guess;
int try = 0;
System.out.println ("Think of a number between 1 to 100");
while (true)
{
if (try == 5)
{
Systems.out.println("You ran out of tries. Sorry round over! ");
break;
}
else
{
while(true)
{
System.out.println("What number did you think of ?");
guess = scanner.nextInt();
try++;
if (guess == yourGuess)
{
System.out.println("Woohoo ! You guessed it right ! Congratulations ! " +yourGuess);
System.out.println ("It took you " + try + "attempts ");
break;
}
elseif (guess < yourGuess)
{
System.out.println("Your guess is a bit low. Guess a little higher number. ");
}
elseif (guess > yourGuess)
{
System.out.println("Your guess is little high. Guess a little lower.");
}
}
}
}
}