forked from mrizky-kur/Redux-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Number_gussing.java
73 lines (37 loc) · 1.26 KB
/
Number_gussing.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
package guessinggame;
* Java game “Guess a Number” that allows user to guess a random number that has been generated.
*/
import javax.swing.*;
publicclassGuessingGame {
publicstaticvoidmain(String[] args) {
int computerNumber = (int) (Math.random()*100 + 1);
int userAnswer = 0;
System.out.println("The correct guess would be " + computerNumber);
int count = 1;
while (userAnswer != computerNumber)
{
String response = JOptionPane.showInputDialog(null,
"Enter a guess between 1 and 100", "Guessing Game", 3);
userAnswer = Integer.parseInt(response);
JOptionPane.showMessageDialog(null, ""+ determineGuess(userAnswer, computerNumber, count));
count++;
}
}
publicstatic String determineGuess(int userAnswer, int computerNumber, int count){
if (userAnswer <=0 || userAnswer >100) {
return"Your guess is invalid";
}
elseif (userAnswer == computerNumber ){
return"Correct!\nTotal Guesses: " + count;
}
elseif (userAnswer > computerNumber) {
return"Your guess is too high, try again.\nTry Number: " + count;
}
elseif (userAnswer < computerNumber) {
return"Your guess is too low, try again.\nTry Number: " + count;
}
else {
return"Your guess is incorrect\nTry Number: " + count;
}
}
}