forked from kishanrajput23/Java-Projects-Collections
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.java
55 lines (52 loc) · 1.41 KB
/
Game.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
import java.util.Random;
import java.util.Scanner;
class Game1{
private int noofGuesses=0;
public int inputno;
private int Rando;
public Game1(){
Random rand = new Random();
int upperbound=100;
Rando= rand.nextInt(upperbound);
}
public void TakeUserinput(){
System.out.print("Guess a no:");
Scanner a1 = new Scanner(System.in);
inputno= a1.nextInt();
a1.close(); //scanner closed
}
public void SetnoofGuesses(int Guesses){
this.noofGuesses=Guesses;
}
public int GetnoofGuesses(){
return noofGuesses;
}
boolean NoofCorrectGuesses(){
noofGuesses++;
if (inputno==Rando){
System.out.printf("You guesses the no right: "+ "%d",Rando);
System.out.println("");
System.out.printf("You guesses in " + "%d" +" attempts",noofGuesses);
System.out.println("");
return true;
}
else if(inputno>Rando){
System.out.println("No is greater");
}
else if(inputno<Rando){
System.out.println("No is less");
}
return false;
}
}
//starting main function
public class Game {
public static void main(String[] args) {
Game1 gaming=new Game1();
boolean b=false;
while (!b) {
gaming.TakeUserinput();
b=gaming.NoofCorrectGuesses();
}
}
}