Java Boolean 類的 logicalAnd() 方法返回對指定的布爾操作數執行邏輯與運算的結果。
用法:
public static boolean logicalAnd(boolean a, boolean b)
參數:
傳遞的參數是 a(第一個布爾操作數)和 b(第二個布爾操作數)。
返回值:
logicalAnd() 方法返回對布爾參數 a 和 b 應用邏輯與運算的結果。
例子1
public class BooleanLogicalAndExample1 {
public static void main(String[] args) {
Boolean b1 = true;
Boolean b2 = true;
// when bool values are true logicalAnd() method returns true
Boolean b3 = Boolean.logicalAnd(b1,b2);
System.out.println("1. logicalAnd() method for "+b1+" "+b2+" is "+b3);
Boolean b4 = true;
Boolean b5 = false;
// if either of the bool value if false logicalAnd() method returns false.
Boolean b6 = Boolean.logicalAnd(b4,b5);
System.out.println("2. logicalAnd() method for "+b4+" "+b5+" is "+b6);
}
}
輸出:
1. logicalAnd() method for true true is true 2. logicalAnd() method for true false is false
例子2
import java.util.Scanner;
public class BooleanLogicalAndExample2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter your percentage.");
Double d1 = scanner.nextDouble();
Boolean b1 = true;
Boolean b2 = false;
if (d1 > 64) {
b2 = true;
// logicalAnd() method will return true bool value
Boolean b3 = Boolean.logicalAnd(b1,b2);
if((b3==true)) {
System.out.println("Congratulation! You have passed.");
}
}
else{
double d2=64.0-d1;
System.out.println("Sorry! You have failed. You needed "+d2+" more marks to pass.\nTry again next year.");
}
}
}
輸出:
Enter your percentage. 63 Sorry! You have failed. You needed 1.0 more marks to pass. Try again next year.
例子3
import java.util.Scanner;
public class BooleanLogicalAndExample3 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter two number.");
int a[] = new int[3];
for (int i = 0; i < 2; i++) {
a[i] = scanner.nextInt();
}
Boolean b1 = true;
Boolean b2 = false;
int val=0;
int rem=0;
for (int i = 0; i < 2; i++) {
if (a[1] == 0) {
b2 = true;
break;
}
else{
val = a[i]/a[i+1];
rem= a[i]%a[i+1];
break;
}
}
//logicalAnd() method will give same result as equals() method
if (Boolean.logicalAnd(b1,b2)) {
System.out.println("Divide = "+a[0]+"/"+a[1]+"\nDivision is not possible.");
} else {
System.out.println("Divide = "+a[0]+"/"+a[1]+"\nAfter division.\nQuotient = "+val +"\nRemainder = "+rem);
}
}
}
輸出:
Enter two number. 0 0 Divide = 0/0 Division is not possible.
相關用法
- Java Boolean logicalXor()用法及代碼示例
- Java Boolean logicalOr()用法及代碼示例
- Java Boolean hashCode()用法及代碼示例
- Java Boolean booleanValue()用法及代碼示例
- Java Boolean toString()用法及代碼示例
- Java Boolean compare()用法及代碼示例
- Java Boolean getBoolean()用法及代碼示例
- Java Boolean valueOf()用法及代碼示例
- Java Boolean parseBoolean()用法及代碼示例
- Java Boolean compareTo()用法及代碼示例
- Java Boolean hashcode()用法及代碼示例
- Java Boolean equals()用法及代碼示例
- Java Guava Booleans.concat()用法及代碼示例
- Java Guava Booleans.countTrue()用法及代碼示例
- Java Booleans.indexOf(boolean[] array, boolean[] target)用法及代碼示例
- Java Guava Booleans.lastIndexOf()用法及代碼示例
- Java Guava Booleans.hashCode()用法及代碼示例
- Java Guava Booleans.asList()用法及代碼示例
- Java Guava Booleans.toArray()用法及代碼示例
- Java Guava Booleans.contains()用法及代碼示例
注:本文由純淨天空篩選整理自 Java Boolean logicalAnd() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。