-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTask2.java
More file actions
58 lines (49 loc) · 2.24 KB
/
Task2.java
File metadata and controls
58 lines (49 loc) · 2.24 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
import java.util.Random;
import java.util.Scanner;
public class Task2 {
private static final int MIN_RANGE = 1;
private static final int MAX_RANGE = 100;
private static final int MAX_ATTEMPTS = 10;
private static final int MAX_ROUNDS = 3;
public static void main(String[] args) {
Random random = new Random();
Scanner scanner = new Scanner(System.in);
int totalScore = 0;
System.out.println("NUMBER GUESSING GAME");
System.out.println("Total Number Of Rounds : 3");
System.out.println("Attempts To Guess Number In Each Round : 10\n");
for (int i = 1; i <= MAX_ROUNDS; i++) {
int number = random.nextInt(MAX_RANGE) + MIN_RANGE;
int attempts = 0;
System.out.printf("Round %d: Guess the number between %d and %d in %d attempts.\n", i, MIN_RANGE, MAX_RANGE,
MAX_ATTEMPTS);
while (attempts < MAX_ATTEMPTS) {
System.out.println("Enter your guess : ");
int guess_number = scanner.nextInt();
attempts = attempts + 1;
if (guess_number == number) {
int score = MAX_ATTEMPTS - attempts;
totalScore = totalScore + score;
System.out.printf("Hurray! Number Guessed Successfully. Attempts = %d. Round Score = %d\n",
attempts, score);
break;
}
else if (guess_number < number) {
System.out.printf("The number is greater than %d. Attempts Left = %d.\n", guess_number,
MAX_ATTEMPTS - attempts);
}
else if (guess_number > number) {
System.out.printf("The number is less than %d. Attempts Left = %d.\n", guess_number,
MAX_ATTEMPTS - attempts);
}
}
if (attempts == MAX_ATTEMPTS) {
System.out.printf("\nRound = %d\n", i);
System.out.println("Attempts = 0");
System.out.printf("The Random Number Is : %d\n\n", number);
}
}
System.out.printf("Game Over. Total Score = %d\n", totalScore);
scanner.close();
}
}