创建各类三角形

一 、使用 * 号

import java.util.Scanner;

public class Triangle {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}
5
*
**
***
****
*****

二 、使用数字

import java.util.Scanner;

public class Triangle {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= i; j++) {
                if (j != i) {
                    System.out.print(j + " ");
                } else {
                    System.out.print(j);
                }
            }
            if (i != n) {
                System.out.println();
            }
        }
    }
}
5
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

三 、使用字母

import java.util.Scanner;

public class Triangle {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        for (int i = 1; i <= n; i++) {
            char x = 'A';
            for (int j = 1; j <= i; j++) {
                if (j != i) {
                    System.out.print(x + " ");
                    x++;
                } else {
                    System.out.print(x);
                    x++;
                }
            }
            if (i != n) {
                System.out.println();
            }
        }
    }
}
5
A
A B
A B C
A B C D
A B C D E

四 、倒三角形

import java.util.Scanner;

public class Triangle {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        for (int i = n; i > 0; i--) {
            for (int j = i; j > 0; j--) {
                if (j != 1) {
                    System.out.print("* ");
                } else {
                    System.out.print("*");
                }
            }
            if (i != 1) {
                System.out.println();
            }
        }
    }
}
5
* * * * *
* * * *
* * *
* *
*

五 、倒三角形数字

import java.util.Scanner;

public class Triangle {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        for (int i = n; i > 0; i--) {
            for (int j = i, k = 1; j > 0; j--, k++) {
                if (j != 1) {
                    System.out.print(k + " ");
                } else {
                    System.out.print(k);
                }
            }
            if (i != 1) {
                System.out.println();
            }
        }
    }
}
5
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1

相关推荐

  1. 创建各类三角形

    2024-03-24 19:32:01       42 阅读
  2. PCL 三角形三角形的距离

    2024-03-24 19:32:01       36 阅读
  3. css实现三角形

    2024-03-24 19:32:01       60 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-24 19:32:01       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-24 19:32:01       106 阅读
  3. 在Django里面运行非项目文件

    2024-03-24 19:32:01       87 阅读
  4. Python语言-面向对象

    2024-03-24 19:32:01       96 阅读

热门阅读

  1. Python:继承

    2024-03-24 19:32:01       43 阅读
  2. C++ 日期 & 时间

    2024-03-24 19:32:01       44 阅读
  3. [falsk]flask加载项目配置的两种方式

    2024-03-24 19:32:01       48 阅读
  4. uniapp离线打包笔记

    2024-03-24 19:32:01       40 阅读
  5. 单元测试框架 Junit

    2024-03-24 19:32:01       44 阅读
  6. 算法刷题记录 Day25

    2024-03-24 19:32:01       43 阅读
  7. 条件约束聚类并显示

    2024-03-24 19:32:01       39 阅读
  8. 若依实现多数据源

    2024-03-24 19:32:01       43 阅读