只有一半的钻石是正确绘制的

时间:2016-06-25 02:00:07

标签: java loops for-loop ascii-art

我想在画框中画出一颗钻石。我想通过上半部分,但是当我到了下半场时,我试图颠倒循环并出现问题。我玩转换操作员只是为了看结果,但仍然无效。请帮忙。我没看到什么。

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

3 个答案:

答案 0 :(得分:0)

我认为你正在努力实现这个结果?

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

}

答案 1 :(得分:0)

首先,我建议保持代码整齐排列在输出方面。当你不这样做时,更难看出哪些代码块产生了什么。这是上半部分的清理版本(但代码完全相同):

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

答案 2 :(得分:0)

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