[英]Draw an ASCII diamond in a frame
I am trying to draw out a diamond in a frame.我想在一个框架中画出一颗钻石。 I figured my way through the top half, but when I come to the 2nd half I had attempted to invert the loops and problems came up.我想通了上半场的方法,但是当我来到下半场时,我试图反转循环并且出现了问题。 I played around switching operators just to see the result, but still nothing works.我尝试切换运算符只是为了查看结果,但仍然没有任何效果。 Please help.请帮忙。 What am I not seeing.什么我没看到。
// 1st Half of Diamond
// Creates Lines
for (int i = 1; i <= 3; i++) {
if (i == 1) {
System.out.print("+");
for (int h = 1; h <= 8; h++) {
System.out.print("-");
}
System.out.print("+" + "\n");
}
System.out.print("|");
// Nested Loop Creates Spaces Left Side
for (int j = 4; j > i; j--) {
System.out.print(" ");
}
System.out.print("/");
// Nested Loop Creates Values Inside
for (int j = 1; j < i; j++) {
if (i % 2 == 0) {
System.out.print("--");
} else if (i == 1) {
System.out.print("\\");
} else {
System.out.print("==");
}
}
System.out.print("\\");
// Nested Loop Creates Spaces Right Side
for (int j = 4; j > i; j--) {
System.out.print(" ");
}
System.out.print("|");
System.out.print("\n");
}
// Midpoint of Diamond
System.out.print("|<------>|" + "\n");
//============================
//****HERE PROBLEMS ARISE****
// 2nd Half of Diamond
// Creates Lines
for (int i = 1; i <= 3; i++) {
System.out.print("|");
// Nested Loop Creates Spaces Left Side
for (int j = 1; j <= i; j++) {
System.out.print(" ");
}
System.out.println("\\");
// Nested Loop Creates Values Inside
for (int j = 1; j < 2; j++) {
System.out.print("+");
for (int h = 1; h <= 8; h++) {
System.out.print("-");
}
System.out.print("+" + "\n");
if (i % 2 == 0) {
System.out.print("-");
} else if (i == 3) {
System.out.print("/");
} else {
System.out.print("=");
}
}
}
I assume you're trying to achieve this result:我假设您正在尝试实现此结果:
public class Diamond {
public static void main(String[] args) {
// 1st Half of Diamond
// Creates Lines
for (int i = 1; i <= 3; i++) {
if (i == 1) {
System.out.print("+");
for (int h = 1; h <= 8; h++) {
System.out.print("-");
}
System.out.print("+" + "\n");
}
System.out.print("|");
// Nested Loop Creates Spaces Left Side
for (int j = 4; j > i; j--) {
System.out.print(" ");
}
System.out.print("/");
// Nested Loop Creates Values Inside
for (int j = 1; j < i; j++) {
if (i % 2 == 0) {
System.out.print("--");
} else if (i == 1) {
System.out.print("\\");
} else {
System.out.print("==");
}
}
System.out.print("\\");
// Nested Loop Creates Spaces Right Side
for (int j = 4; j > i; j--) {
System.out.print(" ");
}
System.out.print("|");
System.out.print("\n");
}
// Midpoint of Diamond
System.out.print("|<------>|" + "\n");
// 2nd Half of Diamond
// Creates Lines
for (int i = 1; i <= 3; i++) {
System.out.print("|");
// Nested Loop Creates Spaces Left Side
for (int j = 1; j <= i; j++) {
System.out.print(" ");
}
System.out.print("\\");
// Nested Loop Creates Values Inside
for (int j = 1; j <= i; j++) {
if (i == 2) {
System.out.print("-");
} else if (i == 1) {
System.out.print("====");
} else {
System.out.print("");
}
}
System.out.print("/");
// Nested Loop Creates Spaces Right Side
for (int j = 0; j < i; j++) {
System.out.print(" ");
}
System.out.println("|");
}
System.out.print("+");
for (int h = 1; h <= 8; h++) {
System.out.print("-");
}
System.out.print("+" + "\n");
}
}
Output:输出:
+--------+
| /\ |
| /--\ |
| /====\ |
|<------>|
| \====/ |
| \--/ |
| \/ |
+--------+
First off, I suggest keeping your code grouped neatly with regard to the output.首先,我建议将您的代码按照输出整齐地分组。 It's just harder to see what blocks of code produce what out put when you don't.当你没有输出时,很难看到哪些代码块产生了什么。 Here's a cleaned up version of the 1st half (but the code is exactly the same):这是第一部分的清理版本(但代码完全相同):
// 1st Half of Diamond
for (int i = 1; i <= 3; i++) {
//TOP OR BOTTOM LINE
{
if (i == 1) {
System.out.print("+");
for (int h = 1; h <= 8; h++) {
System.out.print("-");
}
System.out.print("+" + "\n");
}
}
//INTERIOR LINES
{
System.out.print("|");
// Nested Loop Creates Spaces Left Side
for (int j = 4; j > i; j--) {
System.out.print(" ");
}
System.out.print("/");
// Nested Loop Creates Values Inside
for (int j = 1; j < i; j++) {
if (i % 2 == 0) {
System.out.print("--");
} else if (i == 1) {
System.out.print("\\");
} else {
System.out.print("==");
}
}
System.out.print("\\");
// Nested Loop Creates Spaces Right Side
for (int j = 4; j > i; j--) {
System.out.print(" ");
}
System.out.print("|");
System.out.print("\n");
}
}
The output is as you get输出是你得到的
+--------+
| /\ |
| /--\ |
| /====\ |
Inverting a for loop is usually as simple as reversing the index, so you change the outer loop to for (int i = 3; i >=1; i--)
, which gives an output of:反转 for 循环通常与反转索引一样简单,因此您将外部循环更改为for (int i = 3; i >=1; i--)
,它给出的输出为:
| /====\ |
| /--\ |
+--------+
| /\ |
You'll notice the final line prints to early so you should switch the \\\\TOP OR BOTTOM LINE
block with the \\\\INTERIOR LINES
block, which outputs:您会注意到最后一行打印得较早,因此您应该将\\\\TOP OR BOTTOM LINE
块与\\\\INTERIOR LINES
块切换,它输出:
| /====\ |
| /--\ |
| /\ |
+--------+
Now you just need to switch your back-slashes and forward-slashes.现在你只需要切换你的反斜杠和正斜杠。 The resultant code prints a nice second half:结果代码打印了一个不错的下半部分:
| \====/ |
| \--/ |
| \/ |
+--------+
The code being:代码是:
// 2nd Half of Diamond
for (int i = 3; i >= 1; i--) {
//INTERIOR LINES
{
System.out.print("|");
// Nested Loop Creates Spaces Left Side
for (int j = 4; j > i; j--) {
System.out.print(" ");
}
System.out.print("\\");
// Nested Loop Creates Values Inside
for (int j = 1; j < i; j++) {
if (i % 2 == 0) {
System.out.print("--");
} else if (i == 1) {
System.out.print("/");
} else {
System.out.print("==");
}
}
System.out.print("/");
// Nested Loop Creates Spaces Right Side
for (int j = 4; j > i; j--) {
System.out.print(" ");
}
System.out.print("|");
System.out.print("\n");
}
//TOP OR BOTTOM LINE
{
if (i == 1) {
System.out.print("+");
for (int h = 1; h <= 8; h++) {
System.out.print("-");
}
System.out.print("+" + "\n");
}
}
}
Two nested for loops from -n
to n
and a couple of if else statements .从-n
到n
两个嵌套for 循环和几个if else 语句。 The zero point is in the center of the rhombus.零点位于菱形的中心。
public static void main(String[] args) {
printDiamond(5);
}
static void printDiamond(int n) {
System.out.println("n=" + n);
// vertical axis
for (int i = -n; i <= n; i++) {
// horizontal axis
for (int j = -n - 1; j <= n + 1; j++)
if (j == 0) continue; // skip middle vertical
else if (Math.abs(j) == n + 1) // vertical borders & corners
System.out.print(Math.abs(i) == n ? "+" : "|");
else if (Math.abs(i) == n) // horizontal borders
System.out.print("-");
else if (i == 0 && Math.abs(j) == n) // middle left & right tips
System.out.print(j == -n ? "<" : ">");
else if (Math.abs(i - j) == n) // upper right & lower left edges
System.out.print("\\");
else if (Math.abs(i + j) == n) // upper left & lower right edges
System.out.print("/");
else if (Math.abs(i) + Math.abs(j) < n) // inner rhombus lines
System.out.print((n - i) % 2 != 0 ? "=" : "-");
else // whitespace
System.out.print(" ");
System.out.println(); // new line
}
}
Output:输出:
n=5
+----------+
| /\ |
| /--\ |
| /====\ |
| /------\ |
|<========>|
| \------/ |
| \====/ |
| \--/ |
| \/ |
+----------+
See also: How to print a diamond with a frame?另请参阅:如何打印带框架的钻石?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.