-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDKH_NB_Guess.java
More file actions
119 lines (114 loc) · 2.66 KB
/
DKH_NB_Guess.java
File metadata and controls
119 lines (114 loc) · 2.66 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
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/* Daphne KH and Nick
* Jan 11, 2016
* github practice with random number guessing game
*
* run this file and enter guesses as prompted
* sample input/output:
I have a number between 1 and 100.
Can you guess my number?
Please type your first guess.
Your Guess:
[50]
Too high. Try again.
Your Guess:
[25]
Too low. Try again.
Your Guess:
[38]
Too high. Try again.
Your Guess:
[31]
Too high. Try again.
Your Guess:
[29]
Too high. Try again.
Your Guess:
[27]
Too high. Try again.
Your Guess:
[26]
Excellent! You guessed the number! You guessed in 6 tries.
Do you want to play again? (Y/N):
[Y]
I have a number between 1 and 100.
Can you guess my number?
Please type your first guess.
Your Guess:
[50]
Too low. Try again.
Your Guess:
[75]
Too high. Try again.
Your Guess:
[62]
Too high. Try again.
Your Guess:
[56]
Too low. Try again.
Your Guess:
[59]
Too low. Try again.
Your Guess:
[61]
Excellent! You guessed the number! You guessed in 5 tries.
Do you want to play again? (Y/N):
[N]
>
*/
import java.util.Scanner;
import java.util.Random;
public class Guess
{
//main method, does not expect any arguments
public static void main(String[] args)
{
boolean repeat = true;
while (repeat)
{
//prints intial message
System.out.println("I have a number between 1 and 100.");
System.out.println("Can you guess my number?");
System.out.println("Please type your first guess.");
int guess = -1;
Random ran = new Random();
int answer = ran.nextInt(100) + 1;
int tries = 0;
while (guess != answer)// the user correctly guessing will end the loop
{
guess = getInput();
if (guess < answer)
{
System.out.println("Too low. Try again.");
}
else if (guess > answer)
{
System.out.println("Too high. Try again.");
}
else
{
System.out.println("Excellent! You guessed the number! You guessed in " + tries + " tries.");
}
tries++;
}
repeat = playAgain(); //based on boolean from playAgain, determines whether the user wants to play the game again or not
}
}
//returns the number from the user
private static int getInput()
{
Scanner scan=new Scanner(System.in);
System.out.println("Your Guess: ");
int guess=scan.nextInt();
return guess;
}
//play again method
//returns true if the user enters Y, false otherwise
public static boolean playAgain()
{
Scanner scan=new Scanner(System.in);
System.out.println("Do you want to play again? (Y/N): ");
String guess=scan.nextLine();
//System.out.println(guess.equals("Y"));
return (guess.equals("Y"));
}
}