Java Right-Angled Triangle: Print with Nested Loops
Nested loops can be used to print a right-angled triangle as shown below:
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();
}
}
}
Running the above code will output:
*
* *
* * *
* * * *
* * * * *
You can adjust the value of rows as needed to control the size of the triangle.