day3 java運算子與流程式控制制,day3運算子

來源:互聯網
上載者:User

day3 java運算子與流程式控制制,day3運算子

  賦值運算

  賦值運算子:=,+=,-=,×=,/=,%=

class fuzhiyunsuan{        public static void main(String[] args){                int i1 = 10;                i1 += 3;//即i1 = i1 +3,但是這兩者是有區別的                //例如下面                short s = 10;                //s = s + 3;編譯是無法通過的,會損失精度                //s = (short)(s + 1);//強制修改精度,不建議                s += 1;//即可以實現運算,又不會更改s的資料類型;                System.out.println(s);                /*                 賦值運算子:=,+=,-=,×=,/=,%=                  */                //切記在java中=是賦值操作。                boolean b1 = false;                if(b1=true)                        System.out.println("結果為真");                else                        System.out.println("結果為假");        }}
root@debian:/home/jeff/java_coding/day003# java fuzhiyunsuan 11結果為真

  因為使用i = i+1可能無法通過編譯,所以使用+=的方式賦值更好一些。還有在java中=是賦值,判斷相等使用==。

  邏輯運算子

class logiccal{        public static void main(String[] args){                boolean a = true;                boolean b = false;                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);        }}
root@debian:/home/jeff/java_coding/day003# java logiccal falsefalsetruetruefalsetrue

  &與&&的區別

  &&是短路運算的與,即a&&b,當a為false是無論b是真還是假,結果都是假,故不再運行b,直接跳出計算。

class logicdiff{        public static void main(String[] args){                boolean b = false;                int i1 = 10;                if(b & (i1++) > 0){                        System.out.println("good");                }else{                        System.out.println("bad");                }                System.out.println(i1);                int i2 = 10;                if(b && (i2++) > 0){                        System.out.println("good");                }else{                        System.out.println("bad");                }                System.out.println(i2);}}
root@debian:/home/jeff/java_coding/day003# java logicdiff bad11bad10

  在此處即&左邊為false,右邊i++依然計算了,結果+1,&&右邊未+1。更建議使用&&。

class logicdiff{        public static void main(String[] args){                boolean b = false;                int i1 = 10;                if(b & (10 / 0 == 0){                        System.out.println("good");                }else{                        System.out.println("bad");                }                System.out.println(i1);                int i2 = 10;                if(b && 10/0 == 0){                        System.out.println("good");                }else{                        System.out.println("bad");                }                System.out.println(i2);}}

  此處&在編譯過程中會報錯,但是&&則會通過編譯。

  |與||同理。

  位元運算

class bitcal{        public static void main(String[] args){                int i1 = 31;                System.out.println(i1<<3);                System.out.println(i1<<28);                System.out.println(i1>>2);                System.out.println(i1>>>2);                int i2 = -31;                System.out.println(i2>>2);                System.out.println(i2>>>2);        }}
root@debian:/home/jeff/java_coding/day003# java bitcal 248-26843545677-81073741816

  位移運算可能導致精度丟失。無符號位元運算在負數時變化較大。

  兩個變數換位問題

class exchange{        public static void main(String[] args){                int m = 111;                int n = 233;                System.out.println("m:"+m+"n:"+n);                //1.提供中間變數                int temp = m;                m = n;                n = temp;                System.out.println("m:"+m+"n:"+n);                //2.m,n較大可能會損失精度                m = m+n;                n = m-n;                m = m-n;                System.out.println("m:"+m+"n:"+n);                //3.位元運算                m = m^n;                n = m^n;                m = m^n;                System.out.println("m:"+m+"n:"+n);        }}

  運行結果

root@debian:/home/jeff/java_coding/day003# java exchange m:111n:233m:233n:111m:111n:233m:233n:111

   三元運算式

  格式:(條件運算式)?運算式1:運算式2;

  當條件運算式為真,則運算結果為運算式1,當條件運算式為假,則運算結果為運算式2。

  運算式1和2應當為同種類型。

  三元運算式與if判斷的差別為,三元運算式簡化了if語句,三元運算式必須返回一個結果,if後面的代碼快可以有多個語句。

class sanyuan{        public static void main(String[] args){                int i = 10;                int j = 20;                int max = (i > j)?i:j;                System.out.println("較大值是"+max);                String str = (i > j)?"i大":(i == j)?"相等":"j大";                System.out.println(str);        }}

  運算結果

root@debian:/home/jeff/java_coding/day003# java sanyuan 較大值是20j大

   流程式控制制

  順序結構

  程式自上而下,逐行運行。

  分支結構

  根據條件選擇某條分支運行

  迴圈結構,根據迴圈條件重複性的執行某段代碼。

  if語句的三種格式

if(true){執行代碼塊;}
if(條件運算式){執行代碼塊;}else{執行代碼塊;}
if(條件運算式){執行代碼塊;}else if(條件運算式){執行代碼塊;}。。。else{執行代碼塊;}
import java.util.Scanner;class score{        public static void main(String[] args){                //建立一個scanner對象,用於鍵入數字                Scanner uscore = new Scanner(System.in);                System.out.println("請輸入考試成績");                //調用此對象相應方法,完成鍵盤輸入的值的擷取                //next():表示從鍵盤擷取一個字串,int表示鍵入的是整型數值用於比較                int str = uscore.nextInt();                System.out.println("你的成績是"+str);                if(str > 100 || str < 0){                        System.out.println("你輸入的成績有誤");                }                else{                        if(str == 100){                                System.out.println("第一名");                        }else if(str > 80 && str<=99){                                System.out.println("優秀");                        }else if(str >=60 && str<= 80){                                System.out.println("一般");                        }else{                                System.out.println("要加油啊");                        }                }        }}
root@debian:/home/jeff/java_coding/day003# java score 請輸入考試成績13你的成績是13要加油啊root@debian:/home/jeff/java_coding/day003# java score       請輸入考試成績100你的成績是100第一名root@debian:/home/jeff/java_coding/day003# java score 請輸入考試成績76你的成績是76一般

   switch語句

switch(變數){        case 值1:        case 值2:...        default:}
class switch1{        public static void main(String[] args){                int i = 1;                switch(i){                        case 0:                                System.out.println("零");                        case 1:                                System.out.println("一");                        case 2:                                System.out.println("二");                        default:                                System.out.println("其他");                }        }}

  運行結果

root@debian:/home/jeff/java_coding/day003# java switch1 一二其他

  沒有break退出迴圈體,那麼匹配到分支以後還會繼續執行其下的case語句。

  default是可選的,而且位置是靈活的。

class switch2{        public static void main(String[] args){                int i = 5;                switch(i){                        default:                                System.out.println("其他");                        case 0:                                System.out.println("零");                        case 1:                                System.out.println("一");                        case 2:                                System.out.println("二");                }        }}

  運行結果

root@debian:/home/jeff/java_coding/day003# java switch2 其他零一二
class switch3{        public static void main(String[] args){                int i = 5;                switch(i){                        default:                                System.out.println("其他");                                break;                        case 0:                                System.out.println("零");                                break;                        case 1:                                System.out.println("一");                                break;                        case 2:                                System.out.println("二");                                break;                }        }}
root@debian:/home/jeff/java_coding/day003# java switch3 其他

  switch裡的變數類型可以是:char,byte,short,int,枚舉,String(jdk1.7)

  case的條件只能是值不能是取值範圍。

  本節代碼託管到我的github:day003。  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.