打印矩形
**********
**********
**********
**********
**********
**********
**********
**********
**********
**********
public class Demo3_712 { public static void main(String args[]){ for(int i=1;i<=10;i++){ //控制行 for(int j=1;j<=10;j++){ //控制列 System.out.print("*"); } System.out.println(); } } }
打印竖直角三角形
*
**
***
****
*****
******
*******
********
*********
**********
public class Demo3_712 { public static void main(String args[]){ for(int i=1;i<=10;i++){ //控制行 for(int j=1;j<=i;j++){ //控制列 System.out.print("*"); } System.out.println(); } } }
打印竖直角三角形
**********
*********
********
*******
******
*****
****
***
**
*
public class Demo3_712 { public static void main(String args[]){ for(int i=10;i>=0;i--){ //控制行 for(int j=1;j<=i;j++){ //控制列 System.out.print("*"); } System.out.println(); } } }
打印等腰三角形
*
***
*****
*******
*********
*******
*****
***
*
行数 *的个数 打印空格的个数
1 1
2 3
3 5
4 7
n (2*行数)-1 row-j
有错误:无限死循环
import java.util.Scanner; public class Demo3_712 { public static void main(String[] args) { System.out.println("请输入行数:"); int row = new Scanner(System.in).nextInt(); //循环行数 for(int i = 1;i <= row ;i++ ) { for(int a=1;a<=row-i;a++) { System.out.print(" "); } //循环列数 for(int j = 1;j<=2*i-1;j++) { System.out.print("*"); } //换行 System.out.println(); } for(int i = 1;i <= row ;i-- ) { for(int a=1;a<=row-i;a--) { System.out.print(" "); } //循环列数 for(int j = 1;j<=2*i-1;j--) { System.out.print("*"); } //换行 System.out.println(); } } }
使用for循环打印空心菱形和实心菱形
import java.util.Scanner; public class Demo3_712 { public static void main(String args[]) { // int hangshu = 11; // int yiban = hangshu / 2 + 1; // int yibanduo = hangshu / 2; Scanner sc=new Scanner(System.in); System.out.println("请输入行数:"); int hangshu = sc.nextInt(); int yiban = hangshu / 2 + 1; int yibanduo = hangshu / 2; System.out.println("空心菱形:"); for (int k = 1; k <= yiban; k++) { for (int i = 1; i <= (yiban - k); i++) { System.out.print(" "); } System.out.print("*"); for (int i = 1; i <= ((k - 2) * 2 + 1); i++) { System.out.print(" "); } if (k != 1) { System.out.print("*"); } System.out.println(); } for (int k = yibanduo; k >= 1; k--) { for (int i = 1; i <= (yiban - k); i++) { System.out.print(" "); } System.out.print("*"); for (int i = 1; i <= ((k - 2) * 2 + 1); i++) { System.out.print(" "); } if (k != 1) { System.out.print("*"); } System.out.println(); } System.out.println("实心菱形:"); for (int i = 1; i <= hangshu; i++) { if (i <= hangshu / 2 + 1) { for (int k = 1; k <= hangshu / 2 + 1 - i; k++) { System.out.print(" "); } for (int k = 1; k <= i; k++) { System.out.print("* "); } System.out.println(); } else { for (int k = 1; k <= (i - (hangshu / 2 + 1)); k++) { System.out.print(" "); } for (int k = 1; k <= (2 * (hangshu / 2 + 1) - i); k++) { System.out.print("* "); } System.out.println(); } } } }