先看效果:
代碼:
public class PrintTriangle {
public static void main(String[] args) {
System.out.println("======左邊正三角形======");
printTopLeft(5);
System.out.println("======左邊倒三角形======");
printDownLeft(5);
System.out.println("======打印右正三角形======");
printTopRight(5);
System.out.println("======打印右倒三角形======");
printDownRight(5);
System.out.println("======打印正中三角形======");
printTopCenter(5);
System.out.println("======打印倒中三角形======");
printDownCenter(5);
System.out.println("======打印中正空三角形======");
printTopMidBlank(5);
System.out.println("======打印中倒空三角形======");
printDownMidBlank(5);
System.out.println("======打印正人字狀======");
printTopRen(5);
}
/**//*
* 打印正人字狀
*/
public static void printTopRen(int n){
for(int i = 1; i <= n; i ++){
for(int j = i; j < n; j ++){
System.out.print(" ");
}
//左半部分
for(int j = 1; j <= i; j++){
if(j == 1)
System.out.print(" * ");
else
System.out.print(" ");
}
//右半部分
for(int j = 1; j < i; j++){
if(j == i - 1)
System.out.print(" * ");
else
System.out.print(" ");
}
System.out.println();
}
}
/**//*
* 打印倒空三角形
*/
public static void printDownMidBlank(int n){
for(int i = 1; i <= n; i ++){
for(int j = 1; j < i; j ++){
System.out.print(" ");
}
for(int j = i; j <= n; j ++){
if(i == 1 || j == i|| i == n)
System.out.print(" * ");
else
System.out.print(" ");
}
for(int j = i; j < n; j ++){
if(i == 1 || j == n - 1)
System.out.print(" * ");
else
System.out.print(" ");
}
System.out.println();
}
}
/**//*
* 打印中正空三角形
*/
public static void printTopMidBlank(int n){
for(int i = 1; i <= n; i ++){
for(int j = i; j < n; j ++){
System.out.print(" ");
}
//左半部分
for(int j = 1; j <= i; j++){
if(j == 1 || i == n)
System.out.print(" * ");
else
System.out.print(" ");
}
//右半部分
for(int j = 1; j < i; j++){
if(j == i - 1 || i == n)
System.out.print(" * ");
else
System.out.print(" ");
}
System.out.println();
}
}
/**//*
* 打印倒中三角形
*/
public static void printDownCenter(int n){
for(int i = 1; i <= n; i ++){
for(int j = 1; j < i; j ++){
System.out.print(" ");
}
for(int j = i; j <= n; j ++){
System.out.print(" * ");
}
for(int j = i; j < n; j ++){
System.out.print(" * ");
}
System.out.println();
}
}
/**//*
* 打印正中三角形
*/
public static void printTopCenter(int n){
for(int i = 1; i <= n; i ++){
for(int j = i; j < n; j ++){
System.out.print(" ");
}
//左半部分
for(int j = 1; j <= i; j++){
System.out.print(" * ");
}
//右半部分
for(int j = 1; j < i; j++){
System.out.print(" * ");
}
System.out.println();
}
}
/**//*
* 打印右倒三角形
*/
public static void printDownRight(int n){
for(int i = n; i >= 1; i --){
for(int j = n; j > i; j --){
System.out.print(" ");
}
for(int j = i; j >= 1; j --){
System.out.print(" * ");
}
System.out.println();
}
}
/**//*
* 打印右正三角形
*/
public static void printTopRight(int n){
for(int i = 1; i <= n; i ++){
for(int j = n; j > i; j --){
System.out.print(" ");
}
for(int j = i; j >= 1; j--){
System.out.print(" * ");
}
System.out.println();
}
}
/**//*
* 打印左正三角型
*/
public static void printTopLeft(int n){
for(int i = 1; i <= n; i ++){
for(int j = 1; j <= i; j ++){
System.out.print(" * ");
}
System.out.println();
}
}
/**//*
* 打印左倒三角形
*/
public static void printDownLeft(int n){
for(int i = 1; i <= n; i ++){
for(int j = n; j >= i; j --){
System.out.print(" * ");
}
System.out.println();
}
}
}