-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTask_2.java
More file actions
94 lines (80 loc) · 4 KB
/
Task_2.java
File metadata and controls
94 lines (80 loc) · 4 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import java.util.Scanner;
import java.util.Random;
public class Task_2 {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int num, score = 0, attempts = 5;
System.out.println("Hello!! Welcome to Number Guessing Game");
System.out.println("Rules of the Number Guessing Game:");
System.out.println("Random number is generated from the compiler within the range of 1 to 100.");
System.out.println("You have to guess the number in 5 attempts.");
System.out.println("Scoring Rules:");
System.out.println("5 attempts: 10 points");
System.out.println("4 attempts: 20 points");
System.out.println("3 attempts: 30 points");
System.out.println("2 attempts: 40 points");
System.out.println("1 attempt: 50 points");
// Generate a random number between 1 and 100
Random rand = new Random();
num = rand.nextInt(100) + 1;
int lowerBound = 1, upperBound = 100; // Bounds for clues
boolean guessedCorrectly = false;
for (int i = 1; i <= attempts; i++) {
System.out.println("Attempt " + i + ": ");
System.out.println("Guess the number between " + lowerBound + " and " + upperBound + ":");
int guess = in.nextInt();
if (guess == num) {
System.out.println("Congratulations!! You guessed the number in " + i + " attempts.");
score = (attempts - i + 1) * 10;
System.out.println("Your score is: " + score);
guessedCorrectly = true;
break;
} else if (guess < num) {
System.out.println("The number is greater than " + guess);
lowerBound = Math.max(lowerBound, guess + 1); // Update the lower bound
} else {
System.out.println("The number is less than " + guess);
upperBound = Math.min(upperBound, guess - 1); // Update the upper bound
}
}
if (!guessedCorrectly) {
System.out.println("Sorry!! You have exhausted all your attempts.");
System.out.println("The number was: " + num);
}
System.out.println("Do you want to play again? (yes/no)");
String playAgain = in.next();
while (playAgain.equalsIgnoreCase("yes")) {
num = rand.nextInt(100) + 1; // Regenerate a random number
lowerBound = 1;
upperBound = 100;
guessedCorrectly = false;
for (int i = 1; i <= attempts; i++) {
System.out.println("Attempt " + i + ": ");
System.out.println("Guess the number between " + lowerBound + " and " + upperBound + ":");
int guess = in.nextInt();
if (guess == num) {
System.out.println("Congratulations!! You guessed the number in " + i + " attempts.");
score = (attempts - i + 1) * 10;
System.out.println("Your score is: " + score);
guessedCorrectly = true;
break;
} else if (guess < num) {
System.out.println("The number is greater than " + guess);
lowerBound = Math.max(lowerBound, guess + 1);
} else {
System.out.println("The number is less than " + guess);
upperBound = Math.min(upperBound, guess - 1);
}
}
if (!guessedCorrectly) {
System.out.println("Sorry!! You have exhausted all your attempts.");
System.out.println("The number was: " + num);
}
System.out.println("Do you want to play again? (yes/no)");
playAgain = in.next();
}
System.out.println("Thank you for playing the game!!");
System.out.println("Have a nice day!!");
in.close(); // Close the scanner
}
}