-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaNumberGame.java
More file actions
71 lines (57 loc) · 2.11 KB
/
JavaNumberGame.java
File metadata and controls
71 lines (57 loc) · 2.11 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
import java.util.Scanner;
import java.util.Random;
public class JavaNumberGame
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
Random rd = new Random();
int min_Range = 1;
int max_Range = 100;
int score = 0;
int Attempts_No = 4;
System.out.println("Welcome to the Number Guessing Game!!");
System.out.println(
"You can choose a number between " + min_Range + " and " + max_Range + ". Let's see your luck.");
boolean play_Again = true;
while (play_Again)
{
int NumberToGuess = rd.nextInt(max_Range - min_Range + 1) + min_Range;
int current_Attempt = 1;
boolean guessed_Correctly = false;
while (current_Attempt <= Attempts_No)
{
System.out.print("Attempt " + current_Attempt + "/" + Attempts_No + ": Enter your Number : ");
int userGuess = sc.nextInt();
if (userGuess == NumberToGuess)
{
System.out.println("Bravo, You guessed the correct number.");
guessed_Correctly = true;
score += Attempts_No - current_Attempt + 1;
break;
}
else if (userGuess < NumberToGuess)
{
System.out.println("Too low. Try again.");
}
else
{
System.out.println("Too high. Try again.");
}
current_Attempt++;
}
if (!guessed_Correctly)
{
System.out.println("Sorry, you've run out of Attempts_No. The number was: " + NumberToGuess);
}
System.out.print("Do you want to play again? (yes/no): ");
String playAgainResponse = sc.next();
if (!playAgainResponse.equalsIgnoreCase("yes"))
{
play_Again = false;
}
}
System.out.println("GAME OVER!. Your total score is: " + score);
sc.close();
}
}