温馨提示×

怎么用Java打印输出直角三角形

小亿
140
2024-02-27 16:06:15
栏目: 编程语言

可以使用嵌套循环来打印输出直角三角形,如下所示:

public class PrintTriangle {
    public static void main(String[] args) {
        int rows = 5; // 三角形的行数
        
        for (int i = 1; i <= rows; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print("* ");
            }
            System.out.println();
        }
    }
}

运行以上代码,输出为:

* 
* * 
* * * 
* * * * 
* * * * * 

可以根据需要调整rows的值来控制三角形的大小。

0