JAVA逻辑运算符 作者:马育民 • 2020-01-23 21:34 • 阅读:10058 # 介绍 逻辑运算符:是用来连接 **两个 布尔类型** 的运算符,运算结果是 **布尔值**,即 true 或者 false | 操作符 | 描述 | | ------------------------------- | ------------------------------------------------------------ | | `&&` (shift+7) | 逻辑与,两侧都为true时,结果才为true;有一个为false,结果就为false | | 2个竖线 (shift+回车键上面的按键) | 逻辑或,两侧有一个为true,结果就为true;两个都为false时,结果就为false | | `!` (shift+1) | 逻辑非,取反,原本是true,取反就是false | # 逻辑非 ! 逻辑非,就是取反 ``` System.out.println( ! true ) # false System.out.println( ! false) # true System.out.println( ! (3 > 2) ) # false ``` # 逻辑与 && 逻辑与,两侧都为true时,结果才为true;有一个为false,结果就为false ### 应用场景 女性结婚条件:有房、彩礼,即:2个条件必须 **同时满足**,才结婚 ### 代码 1. 当 `a and b` ,a和b都为 `true`时,结果就是 `true` ``` System.out.println( true and true ) ``` 返回结果如下: ``` true ``` 2. 当 `a and b` ,有一个是 `false`,结果就是 `false` 看下面代码 ``` boolean a = true boolean b = false boolean c = a and b System.out.println(c) ``` 返回结果如下: ``` false ``` ### 例子: 数学成绩是90分,语文成绩85分,进行下面编程: 1. 每科成绩大于等于85分,可以得二等奖学金 2. 每科成绩大于等于90分,可以得一等奖学金 3. 每科成绩大于等于98分,可以得特等奖学金 ``` int shu = 90 int yu = 85 # 比较运算的优先级,比逻辑运算符 高 System.out.println( "是否能得二等奖学金:", shu >= 85 and yu >= 85 ) # true System.out.println( "是否能得一等奖学金:", shu >= 90 and yu >= 90 ) # false System.out.println( "是否能得特等奖学金:", shu >= 98 and yu >= 98 ) # false ``` # 逻辑或 || 逻辑或 两侧都是 `false` ,结果才是 `false`,有一个是`true`,结果就是 `true` ### 应用场景 好老师的标准:要么让我玩游戏,要么让我刷抖音,满足一个就是好老师 ### 代码 1. 当 `a or b` ,a和b都为 `false`,结果就是 `false` 看下面的代码: ``` boolean youxi = false // 不让玩游戏 boolean douyin = false // 不让刷抖音 boolean c = youxi || douyin System.out.println(c) ``` 返回结果如下: ``` false ``` 2. 当 `a or b` ,a或b有一个为 `true`,结果就是 `true` 看下面代码 ``` boolean youxi = true // 让玩游戏 boolean douyin = false // 不让刷抖音 boolean c = youxi || douyin System.out.println(c) ``` 返回结果如下: ``` true ``` ### 例子 数学成绩是90分,语文成绩85分,进行下面编程: 1. 有一科成绩大于等于90分,可以得优秀单科奖学金 2. 有一科成绩大于等于95分,可以得特等单科奖学金 ``` int shu = 90 int yu = 85 # 比较运算的优先级,比逻辑运算符 高 System.out.println( "是否能得优秀单科奖学金:", shu >= 90 or yu >= 90 ) # true System.out.println( "是否能得特等单科奖学金:", shu >= 95 or yu >= 95 ) # false ``` # 例子 ### 练习1 ```java boolean a = true; boolean b = true; System.out.System.out.printlnln("a && b = " + (a&&b)); System.out.System.out.printlnln("a || b = " + (a||b) ); System.out.System.out.printlnln("!a = " + !a); ``` ```java boolean a = false; boolean b = true; System.out.System.out.printlnln(a&&b);//结果:false。 两侧都为true时,结果才为true;有一个为false,结果就为false System.out.System.out.printlnln(a||b);//结果:true。 两侧有一个为true,结果就为true;两个都为false时,结果就为false System.out.System.out.printlnln( !a);//结果:true ``` ```java boolean a = false; boolean b = false; System.out.System.out.printlnln(a&&b);//结果:false。 两侧都为true时,结果才为true;有一个为false,结果就为false System.out.System.out.printlnln(a||b);//结果:false。 两侧有一个为true,结果就为true;两个都为false时,结果就为false System.out.System.out.printlnln( !a);//结果:true ``` ### 练习2 ```java boolean a = 5 > 3; //true boolean b = 4 < 2; //false System.out.System.out.printlnln( a&&b );//结果:false System.out.System.out.printlnln( a||b );//结果:true System.out.System.out.printlnln( !a);//结果:false ``` 原文出处:http://malaoshi.top/show_1EF4qs1EYKZ1.html