mercredi 25 octobre 2017

Creating triangle sequences given a format...why won't my triangles be displayed

I'm trying to figure out what is wrong with my program. There should be four triangles printed as such:

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

1 2 3 4 5
1 2 3 4
1 2 3
1 2
1

        1
      2 1
    3 2 1
  4 3 2 1
5 4 3 2 1

1 2 3 4 5
  1 2 3 4
    1 2 3
      1 2
        1

Here is the code I have written so far:

public static String trianglePatterns(int limit) {
      // Triangle One
      for (int x = 1; x <= limit; x++) {
         for (int y = 1; y <= x; y++) {
            System.out.print(y + " ");
         }
         System.out.print("\n");
      }
      System.out.print("\n");
      // Triangle Two
      while (limit > 0) {
         for (int y = 1; y <= limit; y++) {
            System.out.print(y + " ");
         }
         limit--;
         System.out.println("");
      }
      // Triangle Three
      for (int row = 1; row <= limit; row++) {
         for (int space = 7; space >= 0; space--) {
            System.out.print(" ");
         }
         for (int num = 1; row >= 1; num--) {
            System.out.print(" " + num);
         }
      }
      return"";
   }

I haven't even started the fourth one because I have no idea how to. My program will run and display the first two triangles correctly, but just doesn't include the third when I run it. What is causing the problem, and how do I begin the fourth triangle?

Aucun commentaire:

Enregistrer un commentaire