-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberGuessing.java
More file actions
68 lines (58 loc) · 2.48 KB
/
Copy pathNumberGuessing.java
File metadata and controls
68 lines (58 loc) · 2.48 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
import java.util.*;
public class NumberGuessing
{public static void main(String[] args) {
Scanner s= new Scanner(System.in);
Random random = new Random();
boolean playAgain =true;
int totalScore = 0;
int roundNumber =0;
System.out.println("Welcome to the Guessing game of number??");
System.out.println("Guess the number between 1 and 100.");
while (playAgain)
{
roundNumber++;
int generatedNumber = random.nextInt(100)+1; //number generation
int attemptRemaining = 10;
System.out.println("\n--- Round "+ roundNumber +"---");
System.out.println("You have 10 atempts fo guessing ");
boolean guessedCorrectly = false;
while (attemptRemaining > 0) {
System.out.println("Enter your guess("+ attemptRemaining +"attempts left): ");
int guess;
try {
guess = Integer.parseInt(s.nextLine());
} catch (NumberFormatException e) {
System.out.println("Please enter a valid number!");
continue;
}
if (guess<1|| guess> 100) {
System.out.println("Your guess is Out Of Range! Please guess between 1 and 100");
continue;
}
attemptRemaining--;
if (guess == generatedNumber) {
System.out.println("Congratulations! you guessed the correct number.");
totalScore += attemptRemaining+1;
guessedCorrectly = true;
break;
}
else if (guess < generatedNumber) {
System.out.println("Too low! try again.!");
}
else{
System.out.println("Too high! try again.!");
}
if (!guessedCorrectly){
System.out.println("Game over! the correct number was "+ generatedNumber +".");
}
System.out.println("Your total score so far is: "+ totalScore);
System.out.println("Do you want to play another round? (yes or no)");
String playAgainInput = s.nextLine().trim().toLowerCase();
if (!playAgainInput.equals("yes") && !playAgainInput.equals("y")){
playAgain = false;
}
}
System.out.println("Thank you for playing! Your final scores is: " + totalScore);
s.close();
}
}}