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


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


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