-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNumbergame1.java
56 lines (47 loc) · 1.84 KB
/
Numbergame1.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
55
56
import java.util.Scanner;
import java.util.Random;
class Numbergame1 {
public static void main(String args[]) {
int inputno;
int randomno;
int noofguess;
int noofrounds = 0;
boolean playagain = true;
boolean guessiscorrect;
Scanner sc = new Scanner(System.in);
Random r1 = new Random();
while (playagain && noofrounds < 3) {
noofrounds++;
randomno = r1.nextInt(1, 101);
noofguess = 0;
guessiscorrect = false;
System.out.println("Round " + noofrounds + ": Guess a number between 1 and 100");
while (noofguess < 10 && !guessiscorrect) {
inputno = sc.nextInt();
noofguess++;
if (inputno == randomno) {
System.out.println("Your guess is correct! You guessed it in " + noofguess + " attempts.");
guessiscorrect = true;
} else if (inputno > randomno) {
System.out.println("Your guess is too high. Try again!");
} else {
System.out.println("Your guess is too low. Try again!");
}
}
if (!guessiscorrect) {
System.out.println("Sorry, you have exhausted all your attempts. The correct number was " + randomno + ".");
}
if (noofrounds < 3) {
System.out.println("Do you want to play again (yes/no)?");
String userResponse = sc.next();
if (userResponse.equalsIgnoreCase("yes")) {
playagain = true;
} else {
playagain = false;
}
}
}
System.out.println("Game over");
sc.close();
}
}