-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsole.java
More file actions
71 lines (61 loc) · 1.4 KB
/
Copy pathConsole.java
File metadata and controls
71 lines (61 loc) · 1.4 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package animals;
import java.io.*;
public class Console
{
private static InputStream in;
public static BufferedReader br;
public Console()
{
in = System.in;
System.out.println("Console is ready.");
System.out.println("Enter a command followed by ENTER.");
System.out.print("> ");
br = new BufferedReader(new InputStreamReader(in));
}
public char nextCmd()
{
try{
int i = new Integer(br.readLine()); // ?!?!?!?
if(i >= 1 && i <= 5)
{
return (char)(i+96);
} else {
System.out.println("Bad Command....");
}
}catch(Exception e){System.out.println("errors are fun, please input a number");}
return 'f';
}
public static void main(String[] args)
{
// TODO
// Here you must add code that does the
// following:
// 1) Check if the val is between 1 and 5,
// these are the allowable commands.
// 2) If the val is between 1 and 5, return
// the corresponding character:
// 1 -> 'a'
// 2 -> 'b'
// 3 -> 'c;
// 4 -> 'd'
// 5 ->
// 3) If the val is not between 1 and 5, print
// "Bad command", and try to read the input
// again.
//
// Note: remember to deal with exceptions.
Console whatever = new Console();
char returned;
while(true)
{
returned = whatever.nextCmd();
if(!(returned == 'f'))
{
System.out.println(returned);
System.exit(1);
} else {
continue;
}
}
}
}