-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCh09_42_GuessTheNumber.java
More file actions
65 lines (60 loc) · 2.25 KB
/
Ch09_42_GuessTheNumber.java
File metadata and controls
65 lines (60 loc) · 2.25 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
import java.util.Random;
import java.util.Scanner;
class guessGame{
int compGuess;
int noGuesses = 1;
private int userGuess;
public guessGame(){
Random randomNumber = new Random();
compGuess = randomNumber.nextInt(0,100);
}
public int getCompGuess(){
return compGuess;
}
public void UserInput(){
Scanner input = new Scanner(System.in);
System.out.print("Enter a number you think is right!: ");userGuess = input.nextInt();
if (userGuess>100){
System.out.println("Enter a Number Smaller than 100");
System.exit(1011);
}
else if (userGuess>compGuess){
System.out.println("Your guessed number is Greater than the Computer's number! Try Again\n");
noGuesses++;
UserInput();
}
else if (userGuess<compGuess){
System.out.println("Your guessed number is Smaller than the Computer's number! Try Again\n");
noGuesses++;
UserInput();
}
else if (userGuess == compGuess){
System.out.println();
System.out.println("Congratzz !!! You achieved it !!!");
getNoGuesses();
System.out.println("Computer's Number was: " + getCompGuess());
System.out.println();
System.out.println("|| Made by Vedant ||");
System.out.println("|| Java - 2025 ||");
}
}
public void getNoGuesses(){
System.out.println("No of attempts: " + noGuesses);
}
}
public class Ch09_42_GuessTheNumber {
public static void main(String[] args) {
System.out.println();
System.out.println(" ******");
System.out.println();
System.out.println(" ! Game Rules !");
System.out.println(" | Computer will Guess A Number up-till 100 |");
System.out.println(" | You will have to guess it |");
System.out.println(" | Lower the number of Guesses Better is The Score |");
System.out.println();
System.out.println(" ******");
System.out.println();
guessGame userinp = new guessGame();
userinp.UserInput();
}
}