首页 > 代码库 > For循环练习

For循环练习

 1 public class ForTest {
 2     public static void main(String args[]){
 3         //直角三角形
 4         /*for(int i = 1; i <= 5; i++){
 5             for(int j = 1;j <= i;j++){
 6                 System.out.print("*");
 7             }
 8             System.out.print("\n");
 9         }*/
10         //等腰三角形
11         /*for(int i = 1; i <=5;i++ ){
12             for(int j = 1; j <= 5-i;j++){
13                 System.out.print(" ");
14             }
15             for(int k = 1; k <= 2 * i - 1; k++){
16                 System.out.print("*");
17             }
18             System.out.print("\n");
19         }*/
20         //菱形
21         /*for(int i = 1; i <= 5;i++){
22             for(int j = 1; j <= 5-i; j++){
23                 System.out.print(" ");
24             }
25             for(int k = 1; k <= 2*i-1; k++){
26                 System.out.print("*");
27             }
28             System.out.print("\n");
29         }
30         for(int l = 1; l <= 4; l++){
31             for(int m = 1; m <= l; m++){
32                 System.out.print(" ");
33             }
34             for(int n = 1; n <= 9 - 2 * l; n++){
35                 System.out.print("*");
36             }
37             System.out.print("\n");
38         }*/
39 
40 
41         //求 1!+2!+3!+....+9!+10!
42         /*int sum = 0;
43         for(int i = 1; i <= 10; i++){
44             int n = 1;
45             for(int j = 1;j <= i;j++){
46                 n *= j;
47             }
48             sum += n;
49         }
50         System.out.print(sum);*/
51         //判断素数
52         /*System.out.println("请任意输入一个大于1的整数:");
53         Scanner sc = new Scanner(System.in);
54         int n = sc.nextInt();
55         boolean c = false;
56         for(int i = 2;i <= n-1;i++){
57             if(n % i==0){
58                 c = true;
59             }
60             }
61             if(c){
62                 System.out.println("该数不是素数");
63             }else{
64                 System.out.println("该数是素数");
65             }*/
66         //打印9*9乘法口诀表
67         for(int i = 1;i<=9;i++){
68             int n = 1;
69             int j;
70             for(j = 1; j<=i;j++){
71                 n = i * j;
72                 System.out.print(j+"*"+i+"="+ n+"\t");
73             }
74             System.out.print("\n");
75         }
76         //打印100-200之内的所有素数
77         for(int i = 100; i<=200;i++){
78             int j;
79             for(j = 2; j<=i;j++){
80                 if(i%j==0){
81                     if(i == j){
82                         System.out.println(i); 
83                     }
84                     break;
85                 }
86             }
87         }

 

For循环练习