1. 程式人生 > >四、java中的運算子

四、java中的運算子

/*
*一、 運算子
* 1.算數運算子:+,-,*,/,++,--,%
* 2.關係運算符:<,>,<=,>=,==,!=
* 3.布林運算子:&&,||,&,|,!,^
* 4.位運算子
* 5.賦值運算子:=,+=,-=,*=,/=,%=
* 6.字串運算子:+
* 7.條件運算子:?
* 8.其他運算子:instanceof,new
*
*/

二、算數運算子

+,-,*,/,++,--,%

public class Day12 {
    
    public static void main(String[] args){
        
        
int k=10; k++; System.out.println("k="+k);//11 //++如果出現在變數的後面 int m=10; int e=m++;//如果++出現在變數後面,先賦值再自加1 System.out.println("e="+e);//10 System.out.println("m="+m);//11 //++出現在變數的前面 int f=10; ++f; System.out.println(
"f="+f); int c=10; int d=++c;//++出現在變數前面,先自加1再賦值 System.out.println("d="+d);//11 System.out.println("c="+c);//11 int z=10; System.out.println(z++);//10println中相當於賦值運算xx=z++ System.out.println(z);//11 int y=10; System.out.println(
++y);//11 System.out.println(y);//11 } }

三、關係型運算子

/*
* 關係型運算子:運算結果一定是真或假
* <,>,<=,>=,==,!=
*/

public class Day13 {
    public static void main(String[] args){
        int a=10;
        int b=10;
        //判斷兩個基本的資料型別是否相等,必須使用“==”
        System.out.println(a>b);
        System.out.println(a>=b);
        System.out.println(a<b);
        System.out.println(a<=b);
        System.out.println(a==b);
        System.out.println(a!=b);
    }
    

}

四、布林運算子

/*
* 布林運算子:&&,||,&,|,!,^
* & 邏輯與 兩邊都是true,結果是true
* | 邏輯或 一邊是true,結果就是true
* ! 邏輯非 取反
* ^ 邏輯異或 兩邊只要不一致就是true
*
* && 短路與 用法和&一樣
* || 短路或 用法和|一樣
*
* 布林運算子兩邊的運算元必須是布林型別,整個表示式的運算子結果也是一個布林型別
* 例如:5>3 & 5>5 true
*/

public class Day14 {
    public static void main(String[] args){
        System.out.println(5>3 & 5>5);//true
        System.out.println(5<3 | 5>5);//true
        
        System.out.println(!false);//true
        System.out.println(true^false);//true
        System.out.println(true^true);//false
        
        //關於短路與或:後邊一個表示式不執行
        //什麼時候發生短路與:第一個運算元結果是false的時候
        //什麼時候發生短路或:第一個運算元結果是true的時候

//        int a=10;
//        int b=10;
//        System.out.println(a<b & a++>=b);//&左右兩個表示式都執行
//        System.out.println(a);//11
        
        int a=10;
        int b=10;
        System.out.println(a<b & a++>=b);//&&第一個表示式為真,第二個表示式不執行
        System.out.println(a);//10
    }

}

五、賦值運算子

/*
* 關於賦值運算子
* =
* +=
* -=
* *=
*\/=
* %=
*/

public class Day15 {
    public static void main(String[] args){
        
        //+=
        int i=10;
        i += 10;//相當於i=i+10
        System.out.println(i);//20
        
        i -=5;
        System.out.println(i);//15
        
        i *=3;
        System.out.println(i);//45
        
        i /=5;
        System.out.println(i);//9
        
        i %=4;
        System.out.println(i);//1
        
        //基本賦值運算符合擴充套件運算子的區別:
        byte a=10;
        //編譯無法通過,運算結果是int型別,前邊的變數是byte型別
        //a=a+10;
        //語法:擴充套件運算子不改變運算結果型別,可能損失精度
        a+=10;
        //編譯可以通過,嚴重損失精度
        a+=1000;
        
        
        
    }

}

六、字串連線符

/*
* 關於字串連線運算子
* +運算子可以:
* 1.做加法運算(加號兩邊只要都是數字,加法運算)
* 2.字串拼接(+號兩邊任意一端是字串,一定是字串拼接)
*/

public class Day16 {
    public static void main(String[] args){
        int a=10;
        int b=12;
        
        System.out.println("a+b="+(a+b));//a+b=22
        System.out.println("a+b="+a+b);//a+b=1012
        System.out.println(a+"+"+b+"="+(a+b));
    }
    

}

七、條件運算子之三目運算

/*
* 關於條件運算子:三目運算
* ?:
* 語法:boolean表示式?表示式1:表示式2
* boolean表示式的結果是true,則整個表示式的結果就是表示式1的結果,相反則是表示式2的結果
*/

public class Day17 {
    public static void main(String[] atrgs){
        
        boolean sex=true;
        char c= sex?'男':'女';
        System.out.println(c);
        
        boolean isSuccess=true;
        //char c=isSuccess?'s':'男';
        System.out.println(isSuccess?'s':'男');
        
    }

}

八、條件運算子之7中控制語句

/*
* 關於控制語句7種:
* if,if else
* switch
* for
* while
* do while
* break
* continue
*/

public class Day18 {
    public static void main(String[] args){
        boolean isSucess=true;
        if(isSucess){
            System.out.println("1");
        }else{
            System.out.println("0");
        }
        
        //如果分支語句中只有一條語句,大括號可省略
        //不推薦使用
        if(isSucess)
            System.out.println("1");
        else
            System.out.println("0");
        
        
    }
}

/*
* 關於switch語句
* 1.語法:
* switch(int型別){
*
* case int型別:{
* java語句;
* java語句;
* break;
* case int型別:{
* java語句;
* java語句;
* break;
* default:
* java語句;
* }
* 注意:break;可以沒有
* default也可以沒有
* break;語句如果沒有則發生case穿透現象
* switch後面的括號中可以填寫byte,short,char型別,因為可以自動轉換
*/

public class Day19 {
    public static void main(String[] args){
        
//        long a=1000;
//        switch (a){//error
//        case 100:{
//            System.out.println("1");
//        }
//        }
        
        
        long a=1000;
        switch ((int)a){//error
        case 100:{
            System.out.println("1");
        }
        }
        
//        char c='C';
//        switch(c){
//        case 'A':{
//            System.out.println("2");
//            break;
//        }
//        case 'B':{
//            System.out.println("3");
//            break;
//        }
//        case 'C':{
//            System.out.println("4");//4
//            break;
//        }
//        default:{
//            System.out.println("5");
//        }
//        }
        
        //case穿透現象
        char c='A';
        switch(c){
        case 'A':{
            System.out.println("2");
        }
        case 'B':{
            System.out.println("3");
        }
        case 'C':{
            System.out.println("4");
            break;
        }
        default:{
            System.out.println("5");
        }
        }
        //case合併
        char d='A';
        switch(d){
        case 'A':case 'B':case 'C':{
            System.out.println("4");
            break;
        }
        default:{
            System.out.println("5");
        }
        }
        
    }

}

九、練習題

/*
* 練習題:需求:
* 1.系統給定的成績,成績可以帶小數,0-100
* 2.編寫程式根據學的城市需判斷學生的等級
* [90-100]優秀
* [70-90)良好
* [60-70)及格
* [0-60]不及格
* 只允許用switch
*/

public class Day20 {
    public static void main(String[] args){
        double score=95.5;
        int grade=((int)score)/10;
        //System.out.println(grade);
        switch(grade){
        case 9:case 10:{
            System.out.println("優秀");
            break;
        }
        case 7:case 8:{
            System.out.println("良好");
            break;
        }
        case 6:{
            System.out.println("及格");
            break;
        }
        default:{
            System.out.println("不及格");
        }
        }
    }

}