Java Reference
In-Depth Information
public static void drawLine() {
System.out.print("+");
for (int i = 1; i <= 6; i++) {
System.out.print("-");
}
System.out.println("+");
}
The top half of the hourglass is more complex. Here is a typical line:
| \../ |
There are four individual characters, separated by spaces and dots.
. .
/
bar spaces backslash dots slash spaces bar
Thus, a first approximation in pseudocode might look like this:
for (each of 3 lines) {
write a bar on the output line.
write some spaces on the output line.
write a backslash on the output line.
write some dots on the output line.
write a slash on the output line.
write some spaces on the output line.
write a bar on the output line.
go to a new line of output.
}
Again, you can make a table to figure out the required expressions. Writing the
individual characters will be easy enough to translate into Java, but you need to be
more specific about the spaces and dots. Each line in this group contains two sets of
spaces and one set of dots. Table 2.9 shows how many to use.
The two sets of spaces fit the rule (line - 1) , and the number of dots is (6 - 2
* line) . Therefore, the pseudocode should read
for (line going 1 to 3) {
write a bar on the output line.
write (line 1) spaces on the output line.
write a backslash on the output line.
write (6 2 * line) dots on the output line.
write a slash on the output line.
write (line 1) spaces on the output line.
write a bar on the output line.
go to a new line of output.
}
Search WWH ::




Custom Search