第五天,第六天,周而復始的迴圈
阿新 • • 發佈:2020-12-14
前幾天部落格園出問題了沒法寫部落格,現在解決好了,現在整理下上上週五和上週三學習的內容
學習視訊網址:https://www.bilibili.com/video/BV12J41137hu?p=29
邏輯運算子
1 package operator; 2 3 //邏輯運算子 4 public class demo04 { 5 public static void main(String[] args) { 6 //與(and) 或(or) 非(取反) 7 boolean a = true; 8 boolean b = false; 910 System.out.println("a && b: "+(b&&a));//邏輯與運算,只有兩個都為真,結果才為true 11 System.out.println("a || b: "+(b||a));//邏輯或運算,兩個變數有一個為真,則結果為true 12 System.out.println("!(a && b): "+!(b&&a));//非:如果是真,則變為假,如果是假則變為真 13 14 //短路運算 15 int c = 5; 16 booleand =(c<4)&&(c++<4);//如果(c<4)為假,則直接返回假,不執行後面的(c++<4) 17 System.out.println(d); 18 System.out.println(c); 19 } 20 }
執行結果
位運算
1 package operator; 2 3 public class demo5 { 4 public static void main(String[] args) { 5 /* 6 A = 0011 1100 7 B = 0000 11018 ------------------------------------------------------- 9 A%B = 0000 1100 (都是1才為1,否則為0) 10 A|B = 0011 1101(如果對應為都是0,則為0,否則為1) 11 A^B = 0011 0001 (對應位相同為0,不相同為1) 12 ~B = 1111 0010 (取反) 13 14 例題:2*8怎麼算最快? 15 2*8=16 2*2*2*2 16 <<左移*2 17 >>右移/2 18 效率極高 19 20 0000 0010 2 21 0000 0100 4 22 0000 1000 8 23 0001 0000 16 24 */ 25 System.out.println(2<<3); 26 } 27 }
輸出結果
三元運算子
1 package operator; 2 3 public class demo6 { 4 public static void main(String[] args) { 5 //三元運算子 6 //x?y:z 7 //如果x==true,則結果為y,否則結果為z 8 int score = 55; 9 String type = score < 60?"不及格":"及格"; 10 //if 11 System.out.println(type); 12 } 13 }
執行結果