-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGuessGame.java
More file actions
76 lines (64 loc) · 1.99 KB
/
GuessGame.java
File metadata and controls
76 lines (64 loc) · 1.99 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
package NumberGame;
import java.util.*;
public class GuessGame {
public static int score = 0;
public static Scanner sc = new Scanner(System.in);
public static int randomNumber(){
int max = 100;
int min = 1;
int rand = (int)(Math.random()*(max - min +1 ) + min);
return rand;
}
public static void guessGame(){
int guess = 0;
int r = randomNumber();
do {
System.out.println("Enter your guess: ");
int g = sc.nextInt();
if (g == r) {
System.out.println("You guessed the number!\nYou win!");
guess++;
break;
}
else if (g > r) {
System.out.println("Your guess is too high.");
guess++;
}
else {
System.out.println("Your guess is too low.");
}
guess++;
}while (guess < 20);
if(guess == 20){
System.out.println("You have Reached Limit");
}else{
System.out.println("Number of Guess you have taken : "+ guess);
}
score++;
}
public static void game(){
boolean again = true;
do{
guessGame();
System.out.println(" Do you Want to play again (y/n): ");
String s = sc.next();
if(s.equals("y")){
continue;
}else{
again = false;
}
}while(again);
System.out.println("Your Score : "+ score);
System.out.println("Thanks For Playing ");
}
public static void main(String[] args) {
System.out.println(" Do you want to start the Guessing Game !!!! ");
System.out.println(" For Start Press y");
String x = sc.next();
if(x.equals("y")){
game();
}else{
System.out.println("Thank you !!!");
}
}
}