-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTask1NumberGame.java
More file actions
51 lines (40 loc) · 1.64 KB
/
Task1NumberGame.java
File metadata and controls
51 lines (40 loc) · 1.64 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
import java.util.Scanner;
public class Task1NumberGame {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int chances = 8;
int finals = 0;
boolean playAgain = true;
System.out.println("Welcome Buddy!");
while (playAgain) {
System.out.println("Hey Buddy, you have " + chances + " chances to win the game.");
int rand = getRandN(1, 100);
boolean guess = false;
for (int i = 0; i < chances; i++) {
System.out.println("Chance " + (i + 1) + " - Enter your guess:");
int user = sc.nextInt();
if (user == rand) {
guess = true;
finals++;
System.out.println("You Won it.");
break;
} else if (user > rand) {
System.out.println("Too High");
} else {
System.out.println("Too Low");
}
}
if (!guess) {
System.out.println("Sorry Buddy You Lost Chances. The Number is " + rand);
}
System.out.println("Do you want to play again (y/n)?");
String pA = sc.next();
playAgain = pA.equalsIgnoreCase("y");
}
System.out.println("That's it Buddy, Hope you enjoyed it");
System.out.println("Here is your Score " + finals);
}
public static int getRandN(int min, int max) {
return (int) (Math.random() *(max - min + 1) + min );
}
}