1.打印三角形
public static void main(String[] args) {
//行数
for (int i = 1; i < 4; i++) {
//打印空格
for (int j = 1; j <10-i ; j++) {
System.out.print(" ");
}
//打印三角形
for (int j = 1; j <= (2 * i - 1); j++) {
System.out.print("*");
}
System.out.println();
}
}
2.打印六边形
public static void main(String[] args) {
//上半部分行数
for (int i = 1; i <= 6; i++) {
//上半部分空格
for (int j = 1; j < 10-i; j++) {
System.out.print(" ");
}
//上半部分梯形
for (int j = 1; j <= (4+i*2); j++) {
System.out.print("*");
}
System.out.println();
}
//下半部分,同上
for (int i = 5; i >= 1; i--) {
for (int j = 1; j < 10-i; j++) {
System.out.print(" ");
}
for (int j = 1; j <= (4+i*2); j++) {
System.out.print("*");
}
System.out.println();
}
}
3.打印空心菱形
public static void main(String[] args) {
//上半部分行数
for (int i = 1; i <= 9; i++) {
//上半部分空白处
for (int j = 1; j <= 9-i; j++) {
System.out.print(" ");
}
//打印前半部分边界
System.out.print("*");
//打印中间空三角形
for (int j = 1; j <= (i-1)*2-1; j++) {
System.out.print(" ");
}
//打印后半部分边界
if(i==1){
System.out.println(" ");
}else{
System.out.println("*");
}
}
//下半部分,同上
for (int i = 8; i >= 1; i--) {
for (int j = 1; j <= 9-i; j++) {
System.out.print(" ");
}
System.out.print("*");
for (int j = 1; j <= (i-1)*2-1; j++) {
System.out.print(" ");
}
if(i==1){
System.out.println(" ");
}else{
System.out.println("*");
}
}
}
4.打印正方形中空一个三角形
public static void main(String[] args) {
//打印第一行*号
for (int i = 1; i <= 15; i++) {
System.out.print("*");
}
System.out.println();
//下面整个图形的行数
for (int i = 1; i <= 7; i++) {
//打印前部分倒三角
for (int j = 1; j <= 8-i; j++) {
System.out.print("*");
}
//打印空格三角形
for (int k = 1; k <= 2*i-1; k++) {
System.out.print(" ");
}
//打印后部分倒三角
for (int l = 1; l <= 8-i; l++) {
System.out.print("*");
}
System.out.println();
}
}