我一直在尝试用Java完成这个任务。对于我来说,这是一个复杂的绘图过程。
Q1 Write a simple Java program that prints a staircase or a figure as shown below:
+---+ | | +---+---+ | | | +---+---+---+ | | | | +---+---+---+---+ | | | | | +---+---+---+---+---+ | | | | | | +---+---+---+---+---+
我想出了一个解决方案,但还没有达到一半。这是我想出的代码:
public class DrawStairs {
public static final int HEIGHT = 5;
public static final int TOTALHEIGHT = HEIGHT * 5;
public static void main(String[] args) {
//Main Outer Loop
for (int i = 1; i <= HEIGHT; i++) {
//Loop for the spaces before, then print the head
for (int j = 1; j <= TOTALHEIGHT + (i * (-5)); j++) {
System.out.print(" ");
}
printTop();
//Loop for spaces after, then print asterisk
for (int j = 1; j <= (i - 1); j++) {
System.out.print("---+");
}
System.out.println(" ");
//Loop for the spaces before, then print the body
for (int j = 1; j <= TOTALHEIGHT + (i * (-5)); j++) {
System.out.print(" ");
}
printMiddle();
//Loop for spaces after, then print asterisk
for (int j = 1; j <= (i - 1) * 5; j++) {
System.out.print(" ");
}
//Loop for spaces before, then print the legs
for (int j = 1; j <= TOTALHEIGHT + (i * (-5)); j++) {
System.out.print(" ");
}
printBottom();
//Loop for spaces after, then print asterisk
for (int j = HEIGHT; j <= 0; --j) {
System.out.print("---+");
}
System.out.println("|");
}
// for loop for printing the floor of asterisks
for (int i = 1; i <= HEIGHT; i++) {
System.out.print("+---+");
}
}
public static void printTop() {
System.out.print("+---+");
}
public static void printMiddle() {
System.out.print("| |");
}
public static void printBottom() {
// System.out.print("+---+");
}
}
这就是它的功能。
+---+
| | |
+---+---+
| | |
+---+---+---+
| | |
+---+---+---+---+
| | |
+---+---+---+---+---+
| | |
+---++---++---++---++---+
请问有人能帮助我并指导我的代码吗?如果有人能告诉我哪里出了问题,需要作出什么更改,我会很感激的。