forked from abhimanyuiscoding/Hacktoberfest2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NumberGuessingGame.java
75 lines (60 loc) · 1.66 KB
/
NumberGuessingGame.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Author: ADARSH
import java.util.*;
class random
{
private int n;
public int ran()
{
Random num = new Random();
return num.nextInt(10);
}
public int takeUserInput()
{
Scanner in = new Scanner(System.in);
int i= in.nextInt();
return i;
}
public int setCounter()
{
return n=0;
}
public int setter_numberOfGuesses(int g)
{
return n=++g;
}
public int getter_numberOfGuesses()
{
return n;
}
public boolean isCorrectNumber(int uno,int rno,int gno)
{
if(uno==rno)
{
System.out.printf((gno==1)?"\nWohoo!!!\nYou nailed it in your first shot.\n\n":"\nCongratulations! \nYou guessed the corrrect number in %d attempts.\n\n",gno);
return true;
}
else
{
System.out.println((uno>rno)?"\nYour number is larger than the actual number\nTry Again!":"\nYour number is smaller than the actual number\nTry Again!");
System.out.println();
return false;
}
}
}
public class NumberGuessingGame {
public static void main(String[] args) {
System.out.println("\nEnter a single-digit number:");
random r = new random();
int gno=r.setCounter(),uno;
int rno=r.ran();
// //cheat
// System.out.println("The actual number choosen by computer is "+rno);
// // A gift from ADARSH.
do {
uno=r.takeUserInput();
gno=r.setter_numberOfGuesses(gno);
gno=r.getter_numberOfGuesses();
}
while(!(r.isCorrectNumber(uno,rno,gno)));
}
}