-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHelpClassDemo.java
More file actions
87 lines (79 loc) · 2.15 KB
/
HelpClassDemo.java
File metadata and controls
87 lines (79 loc) · 2.15 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
Project 4-1
Convert the help system from Project 3-3 into a Help class.
*/
class Help {
void helpon(int what) {
switch(what) {
case '1':
System.out.println("The if:\n");
System.out.println("if(condition) statement;");
System.out.println("else statement;");
break;
case '2':
System.out.println("The switch:\n");
System.out.println("switch(expression) {");
System.out.println(" case constant:");
System.out.println(" statement sequence");
System.out.println(" break;");
System.out.println(" // ...");
System.out.println("}");
break;
case '3':
System.out.println("The for:\n");
System.out.println("for(initialization; condition; iteration) statement;");
break;
case '4':
System.out.println("The while:\n");
System.out.println("while(condition) statement;");
break;
case '5':
System.out.println("The do-while:\n");
System.out.println("do {");
System.out.println(" statement;");
System.out.println("} while (condition);");
break;
case '6':
System.out.println("The break:\n");
System.out.println("break; or break label;");
break;
case '7':
System.out.println("The continue:\n");
System.out.println("continue; or continue label;");
break;
}
System.out.println();
}
void showmenu() {
System.out.println("Help on:");
System.out.println(" 1. if");
System.out.println(" 2. switch");
System.out.println(" 3. for");
System.out.println(" 4. while");
System.out.println(" 5. do-while");
System.out.println(" 6. break");
System.out.println(" 7. continue\n");
System.out.print("Choose one (q to quit): ");
}
boolean isvalid(int ch) {
if(ch < '1' | ch > '7' & ch != 'q') return false;
else return true;
}
}
class HelpClassDemo {
public static void main(String args[]) throws java.io.IOException {
char choice;
Help hlpobj = new Help();
for(;;) {
do {
hlpobj.showmenu();
do {
choice = (char) System.in.read();
} while(choice == '\n' | choice == '\r');
} while( !hlpobj.isvalid(choice) );
if(choice == 'q') break;
System.out.println("\n");
hlpobj.helpon(choice);
}
}
}