Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a double mirrored triangle

Tags:

java

I need help making a mirrored triangle like this:

*      *
**    **
***  ***
********

I can get each one seperatly, but I can't combine them.

public static void main(String[] args){
      for( int i = 1; i <= 5; i++ ){
            for( int j = 0; j < i; j++ ){
                System.out.print("*");

            }
            System.out.println();
        }
      for(int i = 0; i < 6; i++)
      {
          for(int j = 5; j > 0; j--)
          {
              if(i < j)
                  System.out.print(" ");
              else
                  System.out.print("*");
          }
          System.out.println();
      }
}
like image 639
Justin Guo Avatar asked Mar 06 '16 18:03

Justin Guo


3 Answers

You need to track the position from both sides to be able to show * at correct location. Here is the solution:

for (int i = 1; i <= 5; i++) {
    for (int j = 1; j <= 10; j++) {
        if (j <= i || j > 10 - i) {
            System.out.print("*");
        } else {
            System.out.print(" ");
        }
    }
    System.out.println();
}
like image 121
Mrinal Avatar answered Nov 12 '22 22:11

Mrinal


You can do it using this code.

public class Test {
    public void sizeOfTri(int triSize) { //Number of lines
        int line = 1;
        for(int i = 0; i < triSize; i++) { //Handles Each line
            int temp = triSize * 2;
            for(int j = 1; j < temp; j++) { //Handles Each space on the line
                if(j <= line && j == triSize || j >= temp - line && j == triSize) { //For the very last line because it needs an extra *
                    System.out.print("**");
                } else if(j <= line || j >= temp - line) { //For normal lines
                    System.out.print("*");
                } else if(j == triSize) {
                    System.out.print("  "); //For the second to last line because it needs an extra space to make it look mirrored
                } else { //For normal lines
                    System.out.print(" ");
                }
            }
            System.out.println();
            line++;
        }
    }

    public static void main(String[] args) {
        new Test().sizeOfTri(4);
    }
}

I commented on next to if statements on which part does what. This will produce an output which looks like the below when run

*      *
**    **
***  ***
********
like image 21
Dan Avatar answered Nov 12 '22 22:11

Dan


Although, all the above implementations are really good ones. I thought of doing it bit differently and make use of 2-D arrays.

package algorithms;

public class DrawMirror {
    static void initialize(String[][] array){
        for (int i=0; i<MAX_X; i++){
            for (int j=0; j < MAX_Y; j++){
                array[i][j] = " ";
            }
        }
    }

    static void draw(String[][] array, int x, int y){
        for (int i=0; i < y; i++){
            array[x][i] = "*";
            array[x][MAX_Y-i-1] = "*";
        }
    }

final static int MAX_X = 4;
final static int MAX_Y = 8;

    public static void main(String[] args) {
        String[][] array = new String[MAX_X][MAX_Y];

        initialize(array);

        for (int i=0; i < MAX_X ; i++){
            draw(array,i,i+1);
        }


        for( int i = 0; i < MAX_X; i++ ){
            for( int j = 0; j < MAX_Y; j++ ){
                System.out.print(array[i][j]);
            }
            System.out.println();
        }
     }
}
like image 2
The Roy Avatar answered Nov 12 '22 23:11

The Roy