2016-03-06 16 views
5

ho bisogno di aiuto fare un triangolo a specchio come questo:Creazione di un doppio triangolo con mirroring

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

posso ottenere ciascuno in modo esclusivo, ma non riesco a combinare.

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(); 
     } 
} 
+1

@ Dan far loro accettare le proprie risposte, non chiedere upvotes –

risposta

3

È possibile farlo utilizzando questo codice.

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

Ho commentato accanto a se le istruzioni su quale parte fa cosa. Questo produrrà un output che appare come il sotto quando eseguire

*  * 
** ** 
*** *** 
******** 
4

È necessario tenere traccia della posizione da entrambi i lati per essere in grado di mostrare * in posizione corretta. Ecco la soluzione:

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(); 
} 
+0

è piaciuto molto il suo approccio al problema ... – Vineeth

1

Il seguente codice è una funzione con altezza variabile.

public static void printDoubleTriangle(int height) { 
    for(int i = 0; i < height; i++) { 
     for(int j = 0; j < 2*height; j++) { 
      if(j <= i || (2*height - j - 1) <= i) { 
       System.out.print("*"); 
      } else { 
       System.out.print(" "); 
      } 
     } 
     System.out.println(); 
    } 
} 
2

Sebbene, tutte le implementazioni di cui sopra sono davvero buone. Ho pensato di farlo in modo diverso e fare uso di array 2D.

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