练习for循环

 1 class Test1 {
 2      static void CFB (int num) {
 3         for(int x = 1; x<=num; x++) {
 4             for(int y = 1 ; y<=num-x+1; y++) {
 5                 System.out.print(x+"*"+y+"="+x*y+" "+"\t");
 6             }
 7             System.out.println("");
 8         }
 9     }
10 }
11 
12 public class Day1 {
13 
14     public static void main(String[] args) {
15         // TODO Auto-generated method stub
16         Test1.CFB(9);
17     }
18 
19 }

 

1 1*1=1     1*2=2     1*3=3     1*4=4     1*5=5     1*6=6     1*7=7     1*8=8     1*9=9     
2 2*1=2     2*2=4     2*3=6     2*4=8     2*5=10     2*6=12     2*7=14     2*8=16     
3 3*1=3     3*2=6     3*3=9     3*4=12     3*5=15     3*6=18     3*7=21     
4 4*1=4     4*2=8     4*3=12     4*4=16     4*5=20     4*6=24     
5 5*1=5     5*2=10     5*3=15     5*4=20     5*5=25     
6 6*1=6     6*2=12     6*3=18     6*4=24     
7 7*1=7     7*2=14     7*3=21     
8 8*1=8     8*2=16     
9 9*1=9     

 

 for(int x = 1; x<=num; x++) {  //决定行数
            for(int y = 1 ; y<=num-x; y--) {  //决定每一行元素数  
                System.out.print(x+"*"+y+"="+x*y+" "+"\t");  
            }
            System.out.println("");  
        }
    }
1 因为有9行 且从元素x从1到9   所以
int x = 1; x<=num; x++
2 因为每行有9-x个元素y从1到9-x 所以
int y = 1 ; y<=num-x+1; y++
 1 class Test1 {
 2      static void CFB (int num) {
 3         for(int x = 1; x<=num; x++) {
 4             for(int y = 1 ; y<x; y++) {
 5                 System.out.print(" ");
 6             }
 7             for(int z = 1 ; z<=num-x+1; z++) {
 8                 System.out.print("* ");
 9             }
10             System.out.println("");
11         }
12     }
13 }
14 
15 public class Day1 {
16 
17     public static void main(String[] args) {
18         // TODO Auto-generated method stub
19         Test1.CFB(9);
20     }
21 
22 }
1 * * * * * * * * * 
2  * * * * * * * * 
3   * * * * * * * 
4    * * * * * * 
5     * * * * * 
6      * * * * 
7       * * * 
8        * * 
9         * 

 

 

 
 
posted @ 2017-10-26 21:54  大菠萝123  阅读(156)  评论(0)    收藏  举报