如何用Java绘制ASCII楼梯? [英] How to draw an ASCII staircase with Java?
本文介绍了如何用Java绘制ASCII楼梯?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在尝试用 Java 完成这项工作.而且这画起来很复杂,至少对我来说是这样.
I've been trying to get this done in Java. And this is a complex thing to draw, at least for me.
Q1 编写一个简单的 Java 程序,打印楼梯或图形,如下所示:
+---+
| |
+---+---+
| | |
+---+---+---+
| | | |
+---+---+---+---+
| | | | |
+---+---+---+---+---+
| | | | | |
+---+---+---+---+---+
我想出了一个解决方案,但它甚至还不到一半.这是我想出的代码
I have come up with a solution but it is not even halfway there. This is the code I have come up with
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("+---+");
}
}
这就是它的作用.
+---+
| | |
+---+---+
| | |
+---+---+---+
| | |
+---+---+---+---+
| | |
+---+---+---+---+---+
| | |
+---++---++---++---++---+
任何人都可以帮助我并指导我使用我的代码吗?我希望有人能告诉我出了什么问题以及应该改变什么.
Can anyone please help me and guide me with my code? I'd like if someone can tell me what's wrong and what should be changed.
推荐答案
这是我对问题的解决方案.谢谢你的拼图;)
This is my solution to the problem. Thanks for the puzzle ;)
public class Staircase {
public static final int SIZE = 5;
public static final int STAIR_WIDTH = 5;
public static final String TREAD = "-";
public static final String RISER = "|";
public static final String NOSING = "+";
public static final String HOLLOW = " ";
public static void main(String[] args)
{
StringBuilder step = new StringBuilder();
for (int i = 0; i < (STAIR_WIDTH - 2); ++i) { step.append(TREAD); }
StringBuilder hollow = new StringBuilder();
for (int i = 0; i < (STAIR_WIDTH - 2); ++i) { hollow.append(HOLLOW); }
StringBuilder tread = new StringBuilder();
for (int i = 0; i < SIZE; ++i) { tread.append(NOSING + step); }
tread.append(NOSING);
StringBuilder riser = new StringBuilder();
for (int i = 0; i < SIZE; ++i) { riser.append(RISER + hollow); }
riser.append(RISER);
for (int i = 0; i < SIZE; ++i) {
int offset = tread.length() - (((STAIR_WIDTH - 1) * i) + STAIR_WIDTH);
printSpaces(offset);
System.out.println(tread.substring(offset));
printSpaces(offset);
System.out.println(riser.substring(offset));
}
System.out.println(tread);
}
public static void printSpaces(int count)
{
for (int i = 0; i < count; ++i)
System.out.print(" ");
}
}
这篇关于如何用Java绘制ASCII楼梯?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文