-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGuessTheNumber.java
More file actions
80 lines (62 loc) · 2.37 KB
/
GuessTheNumber.java
File metadata and controls
80 lines (62 loc) · 2.37 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import java.util.*;
public class GuessTheNumber {
static Scanner sc = new Scanner(System.in);
public static boolean findNumber(int round){
int attempt = 10;
int score = 100;
boolean ans = false;
Random random = new Random();
int randomNumber = random.nextInt(99) +1;
while(attempt>0 && !ans){
System.out.println("Attempt " + (11-attempt)+"/10");
System.out.println("Guess the Number : ");
int guess = sc.nextInt();
if (guess < randomNumber) {
System.out.println("Little Low\n");
attempt -= 1;
} else if (guess > randomNumber) {
System.out.println("Little High\n");
attempt -= 1;
} else {
System.out.println("\nCongratulation you guess the number " + randomNumber + " in " + (11-attempt) + " guesses.");
System.out.println("Your Score is " + attempt * 10 +"\n");
System.out.println("************************\n");
ans = true;
}
}
if(attempt == 0){
System.out.println("You lost the round no. "+ round +".");
return false;
}
return true;
}
public static void main(String[] args) {
// Game game = new Game();
int round = 1;
boolean isGameOn = true;
System.out.println("Press 1 for Start");
System.out.println("Press 2 for Exit");
System.out.print("\nEnter Your choice: ");
int i = sc.nextInt();
System.out.println("************************\n");
if(i == 1) {
while(true) {
System.out.println("Round " + round + " Start...\n");
isGameOn = findNumber(round);
if(isGameOn) {
System.out.println("Press 1 for Next Round");
System.out.println("Press 2 for Exit");
System.out.print("\nEnter Your choice: ");
int j = sc.nextInt();
if (j == 2) {
break;
}
round += 1;
}else{
System.out.println("Game Over !");
break;
}
}
}
}
}