Java Boolean 类的 logicalOr() 方法返回对指定的布尔操作数执行逻辑或运算的结果。
用法:
public static boolean logicalOr(boolean a, boolean b)
参数:
a- 这是第一个布尔操作数
b- 这是第二个布尔操作数
返回值:
logicalOr() 方法返回对布尔参数 a 和 b 应用逻辑或运算的结果。
例子1
public class BooleanLogicalOrExample1 {
public static void main(String[] args) {
Boolean b1 = true;
Boolean b2 = false;
// if either of the bool value is true logicalOr() method will return true.
boolean b3 = Boolean.logicalOr(b1,b2);
System.out.println("1. logicalOr() method for "+b1+" "+b2+" is "+b3);
boolean b4 = false;
boolean b5 = false;
// if both the bool value are true logicalOr() method will return false
boolean b6 = Boolean.logicalOr(b4,b5);
System.out.println("2. logicalOr() method for "+b4+" "+b5+" is "+b6);
}
}
输出:
1. logicalOr() method for true false is true 2. logicalOr() method for false false is false
例子2
import java.util.Scanner;
public class BooleanLogicalOrExample2 {
public static void main(String[] args) {
Boolean bool1 = true;
Boolean bool2 = false;
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number whose table you want to print:");
int n =scanner.nextInt();
if(n<0||n>20){
bool1 = false;
System.out.println("Please enter a valid number between 0-20.");
}
Boolean bool3 =Boolean.logicalOr(bool1,bool2);
if(bool3==true) {
System.out.println("Table of "+n+":");
for (int i = 1; i < 11; i++) {
System.out.println(n +" * "+ i + " = " + n * i);
}
}
}
}
输出:
Enter the number whose table you want to print:119 Please enter a valid number between 0-20.
例子3
import java.util.Scanner;
public class BooleanLogicalOrExample3 {
public static void main(String[] args) {
Scanner scanner =new Scanner(System.in);
System.out.println("Q How many hours are there in a day?");
System.out.print("Ans:"); int a = scanner.nextInt();
Boolean b1 = false;
Boolean b2 = false;
if(a==24){
b1 = true;
}
Boolean bool = Boolean.logicalOr(b1,b2);
if(bool == true){
System.out.println("Answer is right.");
}
else{
// logicalOr() method returns false
System.out.println("Answer is wrong.");
}
}
}
输出:
Q How many hours are there in a day? Ans:67 Answer is wrong.
相关用法
- Java Boolean logicalAnd()用法及代码示例
- Java Boolean logicalXor()用法及代码示例
- 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 logicalOr() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。