-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGreeter.java
More file actions
44 lines (39 loc) · 1.26 KB
/
Greeter.java
File metadata and controls
44 lines (39 loc) · 1.26 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
import java.util.Scanner;
/** Greet person by name (impatiently) */
public class Greeter {
static final Scanner console = new Scanner(System.in);
/** method to print nag messages until thread is interrupted */
public static void nag() {
try {
while( true ) {
Thread.sleep(2000); // wait for user
if (Thread.interrupted()) break;
System.out.println(getNagMessage());
}
} catch (InterruptedException ex) { /* done nagging */ }
}
/** Greet a user on the console, and nag him to be quick about it. */
public static void main(String[] args) {
// Nag runs in a separate thread, so it is
// not blocked by Scanner waiting for user input.
Thread nag = new Thread(Greeter::nag);
nag.start();
System.out.println("What's your name? ");
String name = console.nextLine().trim();
System.out.println("Hello, "+name);
// stop nagging now
nag.interrupt();
}
/** Get a random nag message. */
private static String getNagMessage() {
final String[] nags = {
"Hurry up! Type something.",
"Humans are so slooooow.",
"My dog types faster than you",
"Can you READ? What's your name?",
"Helloooooo? Anyone there?",
"Forgot your own name? Type something!"};
int index = (int)(Math.random()*nags.length);
return nags[index];
}
}