TAA TAA - 5 months ago 9
Java Question

Only Half of Diamond is Drawn Correctly

// 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 Am Trying to Draw out the Diamond in a Frame I Figured my Way Through the Top Half But When I Come to The 2nd Half I Had Attempt to Invert the Loops But Problems Came Up. I Played Around Switching Operators Just To See The Result But Still Nothings Working. Please Help. What Am I Not Seeing.

Answer

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

}