2016-05-12 147 views
1

這是我的空心三角形程序,但實際上我想在其中打印另一個三角形,如下所示。我曾嘗試做for循環,但所有在我今天白白java中的三角形形狀

 ` * 
     *** 
    ** ** 
    ** ** 
    **  ** 
    **  ** 
************** 
***************` 

int z=1; 

    for (int i=0; i<7; i++) 
    { 
    for (int j=7; j>i; j--) 
    { 
     System.out.print(" "); 
    } 
    System.out.print("*"); 

    if (i!=0) 
    { 
     for (int k=1; k<=z; k++) 
     { 
     System.out.print(" "); 
     } 
     System.out.print("*"); 
     z+=2; 
    } 
    System.out.print("\n"); 
    } 

    for (int i=0; i<=z+1; i++) 
    { 
    System.out.print("*"); 
    } 

回答

0

不錯的益智,這裏是你的代碼

int size = 12; 
for (int i = 0; i < size; i++) { 
    if (i == 0) { 
    for (int j = size; j > i - 3; j--) { 
     System.out.print(" "); 
    } 
    System.out.print("*"); 
    } 
    System.out.println(""); 
    for (int j = size; j >= i; j--) { 
    System.out.print(" "); 
    } 
    System.out.print("**"); 
    for (int j = 0; j <= i * 2; j++) { 
    System.out.print(" "); 
    } 
    System.out.print("**"); 
} 
System.out.println(""); 

for (int i = 0; i < (size * 2) + 6; i++) { 
    if (i == 0) { 
    System.out.print(" "); 
    } else { 
    System.out.print("*"); 
    } 
} 
System.out.println(""); 
for (int i = 0; i < (size * 2) + 8; i++) { 
    if (i == 0) { 
    System.out.print(""); 
    } else { 
    System.out.print("*"); 
    } 
}