-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumber.java
54 lines (42 loc) · 2.54 KB
/
number.java
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
import java.util.Scanner; //user input
import java.util.Random; //generating random numbers
public class number {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Random random = new Random();
int MinRange= 1;
int MaxRange= 100; //range of number
int MaxAttempts = 7;
int score = 0;
boolean playAgain = true; // whether the player wants to play another round
System.out.println("Welcome to the Number Guessing Game!\n");
while (playAgain) { //This loop will keep running as long as the player wants to play again (playAgain is true).
int targetNumber = (int)Math.floor(Math.random()*100)+1;
int attempts = 0;
boolean guessedCorrectly = false;//whether the user has guessed the correct number or not.
System.out.println("Guess the number between " + MinRange + " and " + MaxRange + ".");
while (attempts < MaxAttempts && !guessedCorrectly) {
System.out.print("Enter your guess: ");
int userGuess = scanner.nextInt();
attempts++;
if (userGuess == targetNumber) {
System.out.println("Congratulations! You've guessed the correct number in " + attempts + " attempts.");
score += (MaxAttempts - attempts); // Update the score based on the number of attempts
guessedCorrectly = true;
} else if (userGuess < targetNumber) {
System.out.println("Your guess is too low. Please try again.");
} else {
System.out.println("Your guess is too high.Please try again.");
}
}
if (!guessedCorrectly) {
System.out.println("Sorry, you've run out of attempts. The correct number was " + targetNumber + ".");
}
System.out.println("Your score for this round is: " + (MaxAttempts - attempts));//here score is out of 10.
System.out.print("Do you want to play again? (yes/no): ");
String playAgainResponse = scanner.next();
playAgain = playAgainResponse.equalsIgnoreCase("yes");
}
System.out.println("Thankyou for playing! Your total score is " + score + ".");
}
}