-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGuessing.java
More file actions
33 lines (27 loc) · 846 Bytes
/
Copy pathGuessing.java
File metadata and controls
33 lines (27 loc) · 846 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
/**
* Guessing.java
* Demonstrates the use of a block statement in an if-else.
*/
import java.util.*;
public class Guessing
{
/** Plays a simple guessing game with the user. */
public static void main (String[] args)
{
final int MAX = 10;
int answer, guess;
Scanner scan = new Scanner (System.in);
Random generator = new Random();
answer = generator.nextInt(MAX) + 1; // changes scale from 0-9 to 1-10
System.out.print ("I'm thinking of a number between 1 and "
+ MAX + ". Guess what it is: ");
guess = scan.nextInt();
if (guess == answer)
System.out.println ("You got it! Good guessing!");
else
{
System.out.println ("That is not correct, sorry.");
System.out.println ("The number was " + answer);
}
}
}