当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Boolean logicalOr()用法及代码示例


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 logicalOr() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。