三角形-->九九乘法表

使用嵌套循环打印九行*组成的三角形:

*

**

***

......

*********(9个)

 1 public class Triangle {
 2 
 3     /**
 4      * 使用嵌套循环打印九行*组成的三角形
 5      */
 6     public static void main(String[] args) {
 7         for(int row = 0; row < 9; row++){//
 8             for(int col = 0; col <= row; col++){//每行的*个数
 9                 System.out.print("*");//每次循环打印一个*,注意这里不换行
10             }
11             //每行结束,要换行
12             System.out.println();
13         }
14     }
15 
16 }

 延伸:输入要打印的行数,使用嵌套循环打印三角形

 1 import java.util.Scanner;
 2 
 3 public class Triangle {
 4 
 5     /**
 6      * 使用嵌套循环打印九行*组成的三角形
 7      */
 8     public static void main(String[] args) {
 9         //需要用户输入,所以定义扫描器
10         Scanner sc = new Scanner(System.in);
11         //输入行数
12         System.out.println("请输入行数:");
13         int rows = sc.nextInt();
14         //开始打印三角形,注意循环的行数要用输入的数字
15         for(int row = 0; row < rows; row++){//
16             for(int col = 0; col <= row; col++){//每行的*个数
17                 System.out.print("*");//每次循环打印一个*,注意这里不换行
18             }
19             //每行结束,要换行
20             System.out.println();
21         }
22     }
23 
24 }

 延伸:打印九九乘法表(将第一个直角三角形的*换成乘法式子)

 1 import java.util.Scanner;
 2 
 3 public class Triangle {
 4 
 5     /**
 6      * 九九乘法表
 7      */
 8     public static void main(String[] args) {
 9         //需要用户输入,所以定义扫描器
10         Scanner sc = new Scanner(System.in);
11         //输入行数
12         System.out.println("请输入行数:");
13         int rows = sc.nextInt();
14         //开始打印三角形,注意循环的行数要用输入的数字
15         for(int row = 0; row < rows; row++){//
16             for(int col = 0; col <= row; col++){//每行的*个数
17                 int num01 = row + 1;//行数从0开始,乘法从1开始
18                 int num02 = col + 1;
19                 int result = num01 * num02;//乘法计算
20                 System.out.print(num02+"*"+num01+"="+result+"	");//输出结果,注意这里不换行
21             }
22             //每行结束,要换行
23             System.out.println();
24         }
25     }
26 
27 }
原文地址:https://www.cnblogs.com/jinyufanfan/p/6289915.html