-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGuessANumber.java
More file actions
37 lines (33 loc) · 897 Bytes
/
GuessANumber.java
File metadata and controls
37 lines (33 loc) · 897 Bytes
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
// Andy Kozlowski
// Aaron Olkin
// 1/11/15
// Guess a number 4
import java.util.Scanner;
public class GuessANumber {
// Get a number from the user
private static int getInput()
{
Scanner scan = new Scanner(System.in);
int i = scan.nextInt();
return i;
}
public static void main(String[] args) {
System.out.println("I have a number between 1 and 100.");
System.out.println("Can you guess my number?");
int tries = 0;
int number = new java.util.Random().nextInt(99) + 1;
int guess = 0;
while (guess != number) {
tries++;
guess = getInput();
if (guess > number) {
System.out.println("Too high. Try again.");
}
if (guess < number) {
System.out.println("Too low. Try again.");
}
}
System.out.print("Excellent! You guessed the number! You guess it in");
System.out.println(" " + tries + " tries.");
}
}