-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGuess the number game
79 lines (64 loc) · 2.17 KB
/
Guess the number game
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
77
78
79
package com.company;
import java.util.Random;
import java.util.Scanner;
class Game {
private int userinput;
private int computerinput;
public int getComputerinput() {
return computerinput;
}
public void setComputerinput(int computerinput) {
this.computerinput = computerinput;
}
public int getUserinput() {
return userinput;
}
public void setUserinput(int userinput) {
this.userinput = userinput;
}
public int gameX(int computerinputX) {
computerinputX = computerinput;
return computerinputX;
}
public int takeuserInput(int x) {
x = userinput;
return x;
}
public void isCorrectNumber(int a, int b, int c) {
int d = a-b;
d = Math.abs(d);
if (a == b) {
System.out.println("Yay!! You Win Your Guessing Number is Correct\n");
System.out.format("Total Guesses: %d", c + 1);
System.exit(0);
} else if (d >= 11) {
System.out.println("Difference is more than 11,Try Again Later...\n");
System.exit(0);
} else {
System.out.println("Difference Between Both is of 10.Try More All The Best ");
}
}
}
public class guessthenumber_game {
public static void main(String[] args) {
int numberofguesses = 1;
Scanner sc = new Scanner(System.in);
Random rm = new Random();
Game gameon = new Game();
int computerinput = rm.nextInt(100);
gameon.setComputerinput(computerinput);
System.out.print("Enter a number Between 0-100\n");
int userinput = sc.nextInt();
gameon.setUserinput(userinput);
System.out.format("System input: %d \n",gameon.gameX(computerinput));
gameon.isCorrectNumber(userinput, gameon.gameX(computerinput),numberofguesses);
if(userinput!= gameon.gameX(computerinput)){
while (true){
System.out.println("Enter a Number Again");
userinput=sc.nextInt();
gameon.isCorrectNumber(userinput, gameon.gameX(computerinput),numberofguesses);
numberofguesses++;
}
}
}
}