-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyGuessingGame.java
More file actions
42 lines (31 loc) · 1.24 KB
/
MyGuessingGame.java
File metadata and controls
42 lines (31 loc) · 1.24 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
import java.util.*;
// There's a great write up for this at http://www.java-made-easy.com/guessing-game.html.
public class MyGuessingGame {
public static void main(String[] args) {
System.out.println("your name");
Random r = new Random();
boolean playing = true;
Scanner input = new Scanner(System.in);
while(playing) {
int answer = r.nextInt(100)+1;
System.out.println("Answer = " + answer);
System.out.println("I'm thinking of a number between 1 and 100...");
System.out.print("Your guess? ");
int guess = input.nextInt();
int numberOfGuesses = 1;
while(guess != answer) {
if(guess > answer) {
System.out.println("It's lower.");
}else if(guess < answer) {
System.out.println("It's higher.");
}
numberOfGuesses++;
System.out.print("Your guess? ");
guess = input.nextInt();
}
System.out.println("You got it right in " + numberOfGuesses + " guesses!");
System.out.print("Do you want to play again? ");
playing = input.next().toLowerCase().startsWith("y");
}
}
}