-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain_43.java
More file actions
46 lines (46 loc) · 1.28 KB
/
Main_43.java
File metadata and controls
46 lines (46 loc) · 1.28 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
import java.util.Scanner;
import java.util.Random;
public class Main_43 {
Scanner inp=new Scanner(System.in);
Random rand=new Random();
private int compNum;
private int guess;
private int ans;
public Main_43(){
compNum=rand.nextInt(101);
}
public void input(){
System.out.println("Enter your guessed number:");
ans=inp.nextInt();
}
public void correctAns() {
while (ans != compNum) {
if (ans == compNum)
break;
else if (ans > compNum) {
System.out.println("You entered a greater number, try again");
input();
guess++;
} else if (ans < compNum) {
System.out.println("You entered a lower number, try again");
input();
guess++;
}
System.out.println("Correct answer");
}
}
public void setGuess(){
guess=0;
}
public int getGuess(){
return guess;
}
public static void main(String[] args) {
System.out.println("Let us start the game");
Main_43 game=new Main_43();
game.input();
game.setGuess();
game.correctAns();
System.out.println("Number of guesses: "+game.getGuess());
}
}