首页 > 代码库 > day3(if ,while,switch等语句的应用实例)

day3(if ,while,switch等语句的应用实例)

上午练习情况:

package com.chinasoft.javase.AM;

public class myTest {

    public static void main(String[] args) {
        // TODO Auto-generated method stub\
        
        yiYuan();               //调用yiyuan方法
        
        erYuan();              //调用eryuan方法
        
        sanYuan();            //调用sanyuan方法
        
        chengFaBiao();       //调用chengFaBiao方法
         
        qiuHe();             //求1-100的和
        
        jieCheng();          //求10的阶乘
        
        switchTest();        //switch练习
        
        whileTest();          //while练习
        
        xunHuan();            //循环50遍
        
        int a=1;
        
        if(a<2){
            System.out.println(a+"序号:"+"java");
            
        }
        else{
            System.out.println(a+"序号:"+"java");
            
        }
        }


    private static void xunHuan() {
        
        
        /*int b=0;
        while(b<20){
            System.out.println(b+"序号:"+"python");
            b++;
        }*/
    }

    private static void whileTest() {
        int a=1;
        while(a<=9){
            int b=1;
            while (b<=a){
                System.out.print(b+"*"+a+"="+a*b+"   ");
                b++;
            }
              a++;
              System.out.println();
        }
    }
     
    private static void switchTest() {
        char ch=a;
        
        switch(ch){
        case 1:  System.out.println("true");break;
        case 2:  System.out.println("false");break;
        case 97:  System.out.println("A");break;
        default: System.out.println("none");
        }
    }
    private static void jieCheng() {
        int a = 1;
        int b = 1;
        int c = 0;
        while(a<=10){                //条件判断,什么时候while循环结束
            
            b=a*b;
            c=b+c;
            a++;                       
        }
         System.out.println("10的阶乘是:"+b);      //输出b的值
         System.out.println("10的阶乘的和是:"+c);
    }
    private static void qiuHe() {
        int a = 1;
        int b = 0;
        
        while(a<=100){
            
            b=a+b;
            a++;
        }
         System.out.println(b);
    }
    private static void chengFaBiao() {
        for(int a=1;a<=9;a++){
            for(int b=1;b<=a;b++){
                
                System.out.print(b+"*"+a+"="+a*b+"     ");  //输出a*b
            }
            System.out.println();                          //当第二个for循环完后换行
                
        }
    }
    private static void sanYuan() {
        //三元运算
        
        short    a=12;
        double   b=32.41d;
        float    c=(float)b%a;
        int      d=(int)(a>b?a:b);
        double   e=a>b?a:b;
        
        
        
        
        System.out.println("三元运算:"+a);
        System.out.println("三元运算:"+b);
        System.out.println("三元运算:"+c);
        System.out.println("三元运算:"+d);
        System.out.println("三元运算:"+e);
    }
//二元运算
    private static void erYuan() {
        int    a=3;
        float  b=4.2f;
        int    c=a*(int)b;
        float  d=a*b;
        double e=a*b;
        
        
        
        System.out.println("二元:"+a*b);
        System.out.println("二元:"+c);
        System.out.println("二元:"+d);
        System.out.println("二元:"+e);
        System.out.println("二元:"+a/b+a/e);
        System.out.println("二元:"+b%a);
    }
    //一元运算
    private static void yiYuan() {
        int a=1;
        int b=5;
        
        System.out.println("一元a的值:"+(a++));
        System.out.println("一元b的值:"+(b--));
        System.out.println("一元自减a=" + (++a));
        System.out.println("一元自减a=" + (++b));
    }

}

 

 

  下午练习情况:

package com.chinasoft.javase.PM;

public class lianXi {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
           
            
    
         biJiao();               //比较三个数的大小值
          
        zhouJi();                // 周几的问题
        
        chengJi();               //成绩的好坏
            
        jiaoHuanZhi();           //三个值的交换
        
         suiJiShu();             //用随机数运行switch语句
         
         breakJieShu();           //用break来结束当前循环

         continueXunHuan();        //试验continue循环 
         
         fanChengFaBiao();         //反乘法表
             
         }


    private static void fanChengFaBiao() {
        int a=0,b=0;
         
         for(a=1;a<=5;a++){
             for(b=5;a<=b;b--){
                 System.out.print(b+"*"+a+"="+b*a+"   ");
             }System.out.println();
         }
    }


    private static void continueXunHuan() {
        for(int i=1;i<=5;i++){
             if(i%2==0) continue;      //当余数为0时,结束本次循环继续下次循环
             
                System.out.println("i的值是:"+i); 
             
         }
    }


    private static void breakJieShu() {
        for(int n=1;n<=10;n++){
                  if (n%5==0) break;       //当n除以5的余数是0时,结束当整个前循环
                  System.out.println("n="+n);
              }
    }

    private static void suiJiShu() {
        int  a=(int)(Math.random()*6+1);    //定义了一个整形随机数a,取值范围是[1,7)。
         
         switch(a){
         case 1 : System.out.print("今天吃米饭");break;
         case 2 : System.out.print("今天吃炒面");break;
         case 3 : System.out.print("今天不吃");break;
         case 4 : System.out.print("今天喝水");break;
         case 5 : System.out.print("今天吃肉夹馍");break;
         default : System.out.print("今天张戈请吃大餐");
         }System.out.println();
    }

    private static void jiaoHuanZhi() {
        int a=2;
        int b=3;
        int c=5;
        
        System.out.println("a的值是:"+a);
        System.out.println("b的值是:"+b);
        System.out.println("c的值是:"+c);
        
        c=a;
        a=b;
        b=c;
        
        System.out.println("a换后的值是:"+a);
        System.out.println("b换后的值是:"+b);
        System.out.println("c的值是:"+c);
    }

    private static void chengJi() {
        int a=95;
        
        if(a>=60&a<=75){
            System.out.println("成绩合格");
        }else if(a>75&&a<=90){
            System.out.println("成绩良好");
        }else if(a>90&&a<=100){
            System.out.println("成绩优秀");
        }else{
            System.out.println("成绩不合格");
        }
    }

    private static void zhouJi() {
        int a=7;
        
         
        switch(a){
        case 1:     System.out.println("今天是星期一");break;
        case 2:     System.out.println("今天是星期二");break;
        case 3:     System.out.println("今天是星期三");break;
        case 4:     System.out.println("今天是星期四");break;
        case 5:     System.out.println("今天是星期五");break;
        default:    System.out.println("今天休息");
        }
    }

    private static void biJiao() {
        int a=20;
         int b=30;
         int c=50;
         
         if(a<b&&a<c){
             System.out.println("最小值:"+a);              //如果a小于b和c,则输出最小值a
         }
         else if(b<c){
            System.out.println("最小值:"+b);              //a不是最小值,只用比较b和c的大小,如果b小就输出b
            }
         else {
             System.out.println("最小值:"+c);             //反之输出c
             }
    } 
                
}         

 

day3(if ,while,switch等语句的应用实例)