-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHilo
More file actions
33 lines (31 loc) · 1.57 KB
/
Copy pathHilo
File metadata and controls
33 lines (31 loc) · 1.57 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
import java.util.Scanner;
//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
public class Main {
public static void main(String[] args) {
Scanner Input = new Scanner(System.in);
String playAgain = "";
do {
// create a random variable for the user to guess
int theNumber = (int) (Math.random() * 100 + 1);
// System.out.println(theNumber);
int numberOfTries = 0;
int guess = 0;
while (guess != theNumber) {
System.out.println("Guess a number from 1 to 100");
guess = Input.nextInt();
numberOfTries++;
if (guess < theNumber)
System.out.println(guess + " is too low, try again");
else if (guess > theNumber)
System.out.println(guess + " is too high, try again");
else {
System.out.println(guess + " is correct. You win!");
System.out.println("Nice work! you only used " + numberOfTries + " number of attempts");
}
} // End of while loop for guessing
System.out.println("Would you like to play again? (y/n)");
playAgain = Input.next();
} while (playAgain.equalsIgnoreCase("y"));
}
}