打印常见的图形

    科技2024-10-06  19

    1、矩形

    for (int i = 1; i <= 5 ; i++) { // 输出一行里面的5列星星 for (int j = 1; j <= 5 ; j++) { System.out.print("*"); } // 换到下一行 System.out.println(); }

    2、平行四边形

    // 空格加星星组成 for (int i = 0; i <= 5 ; i++) { // 输出每行里面的空格 for (int j = 0; j <= 5-i; j++) { System.out.print(" "); } for (int j = 1; j <=5; j++) { System.out.print("*"); } System.out.println(); }

    3、等腰三角形

    for (int i = 1; i <=5 ; i++) { for (int j = 1; j <=5-i ; j++) { System.out.print(" "); } for (int j = 1; j <= 2*i-1; j++) { System.out.print("*"); } System.out.println(); }

    4、小九九

    for (int i = 1; i <= 9 ; i++) { for (int j = 1; j <= i ; j++) { System.out.print(i+"*"+j+"="+(i*j)+"\t"); } System.out.println(); }

    5、直角三角形

    for (int i = 1;i <= 5;i++){ for (int j = 1;j <= i;j++){ System.out.print("*"); } System.out.println(); }

    6、菱形

    for (int i=1;i <=5;i++){ for (int j=1;j <= 5-i;j++){ System.out.print(" "); } for (int j=1;j <= 2*i-1;j++){ System.out.print("*"); } System.out.println(); } for (int i=1;i <=4;i++){ for (int j =1;j <= i;j++){ System.out.print(" "); } for (int j =1;j <= 9-2*i;j++){ System.out.print("*"); } System.out.println(); }

    7、沙漏

    for (int i = -4;i <= 4;i++){ for (int j = 1;j <= 4-Math.abs(i);j++){ System.out.print(" "); } for (int j = 1;j <= 1+2*Math.abs(i);j++){ System.out.print("*"); } System.out.println(); }

    8、镂空沙漏

    for (int i = -4;i <= 4;i++){ for (int j = 1;j <= 4-Math.abs(i);j++){ System.out.print(" "); } for (int j = 1;j <= 1+2*Math.abs(i);j++){ boolean is = i==-4 || i ==4 || j==1 || j==1+2*Math.abs(i); System.out.print(is ? "*":" "); } System.out.println(); }
    Processed: 0.008, SQL: 8