【问题标题】:Draw an ASCII diamond in a frame在框架中绘制一个 ASCII 菱形
【发布时间】:2016-06-25 02:00:06
【问题描述】:

我正在尝试在框架中画出钻石。我想办法通过上半场,但是当我来到下半场时,我试图反转循环,问题就出现了。我玩了切换运算符只是为了查看结果,但仍然没有任何效果。请帮忙。我没看到什么。

// 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("=");
        }
    }
}

【问题讨论】:

    标签: java loops for-loop ascii-art


    【解决方案1】:

    我假设您正在尝试实现此结果:

    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");
        }
    }
    

    输出:

    +--------+
    |   /\   |
    |  /--\  |
    | /====\ |
    |<------>|
    | \====/ |
    |  \--/  |
    |   \/   |
    +--------+
    

    【讨论】:

    • 这正是我看到我的错误的预期结果。谢谢。
    【解决方案2】:

    首先,我建议根据输出将代码整齐地分组。当你不这样做时,更难看到哪些代码块产生了什么输出。这是上半部分的清理版本(但代码完全相同):

    // 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");
        }
    }
    

    输出是你得到的

    +--------+
    |   /\   |
    |  /--\  |
    | /====\ |
    

    反转 for 循环通常就像反转索引一样简单,因此您将外部循环更改为 for (int i = 3; i &gt;=1; i--),其输出为:

    | /====\ |
    |  /--\  |
    +--------+
    |   /\   |
    

    您会注意到最后一行打印到早期,因此您应该将 \\TOP OR BOTTOM LINE 块与 \\INTERIOR LINES 块切换,输出:

    | /====\ |
    |  /--\  |
    |   /\   |
    +--------+
    

    现在您只需要切换反斜杠和正斜杠即可。结果代码打印出漂亮的后半部分:

    | \====/ |
    |  \--/  |
    |   \/   |
    +--------+
    

    代码是:

    // 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");
            }
        }
    }
    

    【讨论】:

      【解决方案3】:

      -nn 的两个嵌套for 循环 和几个if else 语句。零点在菱形的中心。

      Try it online!

      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
          }
      }
      

      输出:

      n=5
      +----------+
      |    /\    |
      |   /--\   |
      |  /====\  |
      | /------\ |
      |<========>|
      | \------/ |
      |  \====/  |
      |   \--/   |
      |    \/    |
      +----------+
      

      另见:How to print a diamond with a frame?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-22
        • 2022-01-09
        • 2020-11-18
        相关资源
        最近更新 更多