Java打印实心、空心的三角形和菱形

Java打印实心、空心的三角形和菱形

注:本人之前在博客园也发布过此贴,这次是移植过来,为原创

1.实心三角形

代码:

import java.util.Scanner;

public class Test {

    public static void main(String[] args) {
        int rows;
        Scanner sc = new Scanner(System.in);
        System.out.println("Please input rows:");
        rows = sc.nextInt();
        sc.close();
        for (int i = 1; i <= rows; i++) {//控制打印行数
            for (int j = 1; j <= rows - i; j++) {//控制每行打印空格数
                System.out.print(" ");
            }
            for (int j = 1; j <= i * 2 - 1; j++) {//控制打印星号数
                System.out.print("*");
            }
            System.out.println();//每打一行,换行
        }
    }

}

2.空心三角形

代码:

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        int rows;
        Scanner sc = new Scanner(System.in);
        System.out.println("Please input rows:");
        rows = sc.nextInt();
        sc.close();
        for (int i = 1; i <= rows; i++) {
            for (int j = 1; j <= rows - i; j++) {
                System.out.print(" ");
            }
            for (int j = 1; j <= 2 * i - 1; j++) {
                if (i == 1 || i == rows) { // 如果是第一行或最后一行,打印所有星号
                    System.out.print("*");
                } else if (j == 1 || j == 2 * i - 1) { // 如果是每行的第一个或者最后一个,打印星号
                    System.out.print("*");
                } else { // 其余打印空格
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
    }
}

3.实心菱形

代码:

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        int rows;
        Scanner sc = new Scanner(System.in);
        System.out.println("Please input odd rows:");// 只能是奇数行
        rows = (sc.nextInt() + 1) / 2;// 上半部分行数
        sc.close();
        for (int i = 1; i <= rows; i++) {
            for (int j = 1; j <= rows - i; j++) {
                System.out.print(" ");
            }
            for (int j = 1; j <= 2 * i - 1; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
        for (int i = rows - 1; i >= 1; i--) { // 下半部分不可重复打印上半部分最后一行,i=rows-1)
            for (int j = 1; j <= rows - i; j++) {
                System.out.print(" ");
            }
            for (int j = 1; j <= 2 * i - 1; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

4.空心菱形

代码:

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        int rows;
        Scanner sc = new Scanner(System.in);
        System.out.println("Please input odd rows:");// 只能是奇数行
        rows = (sc.nextInt() + 1) / 2;// 上半部分行数
        sc.close();
        sc.close();
        for (int i = 1; i <= rows; i++) {
            for (int j = 1; j <= rows - i; j++) {
                System.out.print(" ");
            }
            for (int j = 1; j <= 2 * i - 1; j++) {
                if (i == 1) {
                    System.out.print("*");
                } else if (j == 1 || j == 2 * i - 1) {
                    System.out.print("*");
                } else {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
        for (int i = rows - 1; i >= 1; i--) {// 此处只需i=rows-1即可
            for (int j = 1; j <= rows - i; j++) {
                System.out.print(" ");
            }
            for (int j = 1; j <= 2 * i - 1; j++) {
                if (i == 1) {
                    System.out.print("*");
                } else if (j == 1 || j == 2 * i - 1) {
                    System.out.print("*");
                } else {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
    }
}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值