Practice| 流程控制

若整数a除以非零整数b,商为整数,且余数为零,

我们就说a能被b整除(或说b能整除a),a为被除数b为除数,即b|a("|"是整除符号),读作"b整除a"或"a能被b整除。

如:6 | 3,  6能被3整除 或 3能整除6。

if ( x = y ) {  //--->>报编译的错误

  } 

/*
从键盘分别输入年、月、日,判断这一天是当年的第几天 
   注:判断一年是否是闰年的标准:
       1)可以被4整除,但不可被100整除
       2)可以被400整除
要求:使用switch case实现

class YearMonthDay{
    public static void main(String[] args){
        java.util.Scanner input = new java.util.Scanner(System.in);
        System.out.print("年:");
        int year = input.nextInt();
        
        System.out.print("月:");
        int month = input.nextInt();
        
        System.out.print("日:");
        int day = input.nextInt();
        
        int days = day;
        //月份天数的累加
        switch (month){
            case 12:
                days += 30; //前11个月份的天数
            case 11:
                days += 31;//10月份;
            case 10:
                days += 30;//9月份
            case 9:
                days += 31;//8月份
            case 8:
                days += 31;//7月份
            case 7:
                days += 30;//6月份
            case 6:
                days += 31;//5月份
            case 5:
                days += 30;//4月份
            case 4:
                days += 31;//3月份
            case 3:
                days += 28;//2月份
                if((year % 4 == 0 && year % 100 != 0 || year % 400 == 0)){
                    days++;
                }
            case 2:
                days += 31;//1月份
            
            
        }
        //if((month == 3) && (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)){
            //    days++;  行不通的,如果是month=12,它是不会走这一步的。
            //}
        
        System.out.println(year +"年" + month + "月"+ day + "日" +"至今已过" + days);
        
        
    }
}

*/


/*
2、从键盘分别输入年、月、日,判断这一天是当年的第几天 
   注:判断一年是否是闰年的标准:
       1)可以被4整除,但不可被100整除
       2)可以被400整除
要求:使用循环实现

class YearMonthDay{
    public static void main(String[] args){
        java.util.Scanner input = new java.util.Scanner(System.in);
        
        System.out.print("年:");
        int year = input.nextInt();
        System.out.print("月:");
        int month = input.nextInt();
        System.out.print("日:");
        int day = input.nextInt();
        int days = day; //累加零头的天数;
        for(int i = 1; i < month; i++){  //累加满月[2~month-1]的天数; 从2月开始计算;;;;
            if(i == 4 || i == 6 || i == 9 || i == 11){
                days += 30; 
                
            }else if(i == 2){   //注意注意注意,这里是else if,写成if就是它就会在i==4,6,9,11走else多加31天。
                if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0){
                days += 29;
                }else{
                    days += 28; 
                }
            }else{
                days += 31; 
            }
        
        }System.out.println(year + "年" + month +"月" + "至今已过" + days);
    }
}
*/



/*
从键盘分别输入年、月、日,判断这一天是当年的第几天 
   注:判断一年是否是闰年的标准:
       1)可以被4整除,但不可被100整除
       2)可以被400整除
要求:使用循环 + 数组实现
*/
class YearMonthDay{
    public static void main(String[] args){
        java.util.Scanner input = new java.util.Scanner(System.in);
        System.out.print("年:");
        int year = input.nextInt();
        System.out.print("月:");
        int month = input.nextInt();
        System.out.print("日:");
        int day = input.nextInt();
        
        int days = day;
        
        int[] dayOfMonth = {31,28,31,30,31,30,31,31,30,31,30,31};//静态初始化
        for(int i = 1;i < month;i++){
            days += dayOfMonth[i-1];
            if((i == 2) && (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)){
                days++;
            }
        }
        System.out.println(year +"年" + month + "月"+ day + "日" +"至今已过" + days + "天" + "仅剩" + (365-days) +"天");
        
    }
    
}
class Forloop{
    public static void main(String[] args){
        /*
        int sum = 0;
        for(int i = 1; i <=100; i++){
            sum += i;
        }
        System.out.println(sum);
        */
        
        /*
        int count = 0;
        for (int i = 1; i <= 100;i++){
            
            if(i % 3 == 0){
                System.out.print(i + "	");
                count++;
                if(count % 5 == 0){
                    System.out.println("
");
                }
            }
        }
        */
        //方法一
        /*
        for (int i = 1, mi = 0; i <= 1024; i*=2, mi++){
            System.out.println("2的" + mi +"方为:" + i);
        }
        2的0方为:1
        2的1方为:2
        2的2方为:4
        2的3方为:8
        2的4方为:16
        2的5方为:32
        2的6方为:64
        2的7方为:128
        2的8方为:256
        2的9方为:512
        2的10方为:1024
        */
        
        //方法二
        /*
        2的0方为:1
        2的1方为:2
        2的2方为:4
        2的3方为:8
        2的4方为:16
        2的5方为:32
        2的6方为:64
        2的7方为:128
        2的8方为:256
        2的9方为:512
        2的10方为:1024
        */
        
        int result = 0;
        for(int i = 0; result < 1024; i++){
            result = (int) Math.pow(2,i);
            System.out.println("2的" + i +"方为:" + result);
        } 
        
    }
}

九乘九乘法口诀

九九乘法表
1*1=1
1*2=2   2*2=4
1*3=3   2*3=6   3*3=9
1*4=4   2*4=8   3*4=12  4*4=16
1*5=5   2*5=10  3*5=15  4*5=20  5*5=25
1*6=6   2*6=12  3*6=18  4*6=24  5*6=30  6*6=36
1*7=7   2*7=14  3*7=21  4*7=28  5*7=35  6*7=42  7*7=49
1*8=8   2*8=16  3*8=24  4*8=32  5*8=40  6*8=48  7*8=56  8*8=64
1*9=9   2*9=18  3*9=27  4*9=36  5*9=45  6*9=54  7*9=63  8*9=72  9*9=81
*/
class Test10{
    public static void main(String[] args){
        //外层循环,一共有9行
        for(int i = 1;i <= 9; i++){
            //换行之前,要打印该行的n个式子
            /*
            当i=1, j=1,执行内层循环体,跳出内层
            当i=2, j=1,2
            当i=3, j=1,2,3*3
            ...
            当i=9,j=1,2,3,4,5,6,7,8,9
            */
            for(int j = 1;j <= i;j++ ){
                System.out.print(j + "*" + i + "=" + (j*i) + "	");
            }
            System.out.println();
        }
    
    }
}

打印 质数

/*
找出1-100之间所有的素数(质数)

*/
class TestSu12{
    public static void main(String[] args){
        //方法1统计除i和它本身的约数的个数;
        /*
        for(int i = 2;i<=100; i++){
            int count = 0;
            for(int j = 2;j < i;j++){
                if(i % j == 0){ //j是i的约数
                    count++; 
                }
            }if(count == 0){
                System.out.println(i);
            }
        }
        
        
        
        for(int i = 2;i <= 100;i++){
            
            //方法二找除了1和它本身以外约数的证据,找到就停止;定义一个boolean 
            boolean flag = true;
            for(int j = 2; j<i;j++){
                if(i%j == 0){     
                    flag = false; //找到就终止这一层的循环;
                    break; 
                }
                
            }if(flag == true){  //注意注意这里是 == 
                System.out.println(i);
            }
        }
        
        */
        
        for(int i = 2; i<=100;i++){
            
            int j;
            for(j = 2;j<i; j++){
                if(i%j == 0){   //满足i%j == 0了,内循环从break除了 j<i
                    break;         //(i%j==0)没有满足过,内循环的结束  j==i
                }
            }if(j == i){
                System.out.println(i);
            }
        }
        
    }
}

打印菱形

/*                                       i    j    k 
                *                        1    4    1    
            *   *    *                   2    3    3    
        *   *   *    *    *              3    2    5    
    *   *   *   *    *    *    *         4    1    7    
*   *   *   *   *    *    *    *    *    5    0    9    
    *   *   *   *    *    *    *         1    1    7    2*(4-1)+1
        *   *   *    *    *              2    2    5    2*(4-2)+1
            *   *    *                   3    3    3    2*(4-3)+1
                *                        4    4    1    2*(4-4)+1


//方法一先打印上半部分,再打印下半部分;
class TestLingXing{
    public static void main(String[] args){
        //上半部分
        for(int i = 1; i <= 5;i++){
            for(int j = 1; j <= (5-i); j++){
                System.out.print(" ");
            }for(int k = 1; k <= (2*i-1); k++){
                System.out.print("*");
            }
            System.out.println();
        //下半部分    
        }for(int i = 1; i <= 4;i++){
            for(int j = 1;j <= i;j++){
                System.out.print(" ");
            }for(int k = 1; k <= 2*(4-i)+1;k++){
                System.out.print("*");
            }
            System.out.println();
            
        }
    }
}
*/



class TestLingXing{
    public static void main(String[] args){
        for(int i = 1; i <= 9;i++){
            //每一行:(1)打印空格(2)打印*(3)换行
            //(1)打印空格
                    //上半+下半部分 --> " "
            if(i <= 5){//i=1,j=1,2,3,4打印4次;      j <= (5-i)
                       //i=2,j=1,2,3打印3次;
                       //i=3,j=1,2打印2次;
                       //i=4,j=1打印1次;
                       //i=5,j=0不打印;
                for(int j = 1; j <= (5-i); j++ ){
                    System.out.print(" ");
                }  //括号括号括号
            }else{ //i = 6,j=1打印1次;               j <= i - 5
                   //i = 7,j=1,2打印2次;
                   //i = 8,j=1,2,3打印3次;
                   //i = 9,j=1,2,3,4打印4次;
                   
                for(int j = 1; j <= i - 5;j++){
                    System.out.print(" ");
                }                
            }
            //(2)打印*
                 //上半+下半部分 --> "*"    
            if(i <= 5){  //i=1,j=1打印1次;           j<=2*i-1
                         //i=2,j=1,2,3打印3次;
                         //i=3,j=1,2,3,4,5打印5次;
                         //i=4,j=1,2,3,4,5,6,7打印7次;
                         //i=5,j=1,2,3,4,5,6,7,8,9打印9次;
                for(int j = 1;j <= (2*i-1); j++ ){
                    System.out.print("*");
                }
                
            }else{ //i=6,j=1,2,3,4,5,6,7打印7次;     j<=2*(9-i)+1
                   //i=7,j=1,2,3,4,5打印5次;
                   //i=8,j=1,2,3打印3次;
                   //i=9,j=1打印1次;
                   
                for(int j = 1; j <= 2*(9-i)+1;j++){
                    System.out.print("*");
                }
            }
            //(3)打印空格
            System.out.println();
        }
            
    }
}



/*

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

//内循环*只打印首尾,在上边图形的基础上做下判断即可。
class TestLingXing{
    public static void main(String[] args){
        //上半部分
        for(int i = 1; i <= 5;i++){
            for(int j = 1; j <= (5-i); j++){
                System.out.print(" ");
            }for(int k = 1; k <= (2*i-1); k++){
                if(k == 1 || k == (2*i-1)){
                    System.out.print("*");
                }else{
                    System.out.print(" ");
                }
            }
            System.out.println();
        //下半部分    
        }for(int i = 1; i <= 4;i++){
            for(int j = 1;j <= i;j++){
                System.out.print(" ");
            }for(int k = 1; k <= 2*(4-i)+1;k++){
                if(k == 1 || k == 2*(4-i)+1){
                    System.out.print("*");
                }else{
                    System.out.print(" ");
                }
            }
            System.out.println();
            
        }
    }
}
*/
原文地址:https://www.cnblogs.com/shengyang17/p/9971081.html