Java邏輯運算子如何運用?

在Java程式中,需要進行大量的計算,所以要使用到運算子號,下面先來給大家說明Java邏輯運算子如何運用

工具/原料

Eclipse

方法/步驟

賦值運算子是為變數指定具體值的符號,順序是從左到右。

Java邏輯運算子如何運用

下面說明短路與“&&”的運用

首先我們輸入以下程式碼:

public class Test {

public static void main(String[] args) {

boolean a = true;

boolean b = false;

System.out.println("a && b:"+(a&&b));

}

}

會看到輸出結果:a && b:false

Java邏輯運算子如何運用

下面說明短路或“ ”的運用

首先我們輸入以下程式碼:

public class Test {

public static void main(String[] args) {

boolean a = true;

boolean b = false;

System.out.println("a b:"+(a b));

}

}

會看到輸出結果:a b:true

Java邏輯運算子如何運用

下面說明非“!”的運用

首先我們輸入以下程式碼:

public class Test {

public static void main(String[] args) {

boolean a = true;

System.out.println("!a :"+(!a));

}

}

會看到輸出結果:!a :false

Java邏輯運算子如何運用

下面說明或“&”的運用

首先我們輸入以下程式碼:

public class Test {

public static void main(String[] args) {

boolean a = true;

boolean b = false;

System.out.println("a & b :"+(a&b));

}

}

會看到輸出結果:a & b :false

Java邏輯運算子如何運用

下面說明或“ ”的運用

首先我們輸入以下程式碼:

public class Test {

public static void main(String[] args) {

boolean a = true;

boolean b = false;

System.out.println("a b :"+(a b));

}

}

會看到輸出結果:a b :true

Java邏輯運算子如何運用

下面說明異或“^”的運用

首先我們輸入以下程式碼:

public class Test {

public static void main(String[] args) {

boolean a = true;

boolean b = false;

System.out.println("a ^ b :"+(a^b));

}

}

會看到輸出結果:a ^ b :true

Java邏輯運算子如何運用

注意事項

本教程只介紹了常用邏輯運算子的運用

&&與&在功能上本身沒有區別

相關問題答案