介绍了 JavaScript if/else 决策概念的基础知识。
JavaScript 的一个已知功能是决策功能。 可以使用它来控制代码的运行顺序,并使其更易于重复使用且更可靠。
如果要通过代码定义不同的路径,请使用运算符和布尔变量来执行此操作。
- let myTrueBool = true;
- let myFalseBool = false;
布尔值只能是两个值:true 或 false。
布尔值以英国数学家、哲学家和逻辑学家 George Boole (1815–1864) 命名。
运算符通过对创建布尔值进行比较来评估条件。 以下列表描述了经常使用的运算符。
| 符号 | 说明 | 示例 |
|---|---|---|
< | 小于:比较两个值,如果左侧的值小于右侧的值,则返回 true 布尔数据类型 | 5 < 6 // true |
<= | 小于或等于:比较两个值,如果左侧的值小于或等于右侧的值,则返回 true 布尔数据类型 | 5 <= 6 // true |
> | 大于:比较两个值,如果左侧的值大于右侧的值,则返回 true 布尔数据类型 | 5 > 6 // false |
>= | 大于或等于:比较两个值,如果左侧的值大于或等于右侧的值,则返回 true 布尔数据类型 | 5 >= 6 // false |
=== | 全等:比较两个值,如果右侧和左侧的值相等且具有相同的数据类型,则返回 true 布尔数据类型 | 5 === 6 // false |
!== | 不等:比较两个值,并返回全等运算符要返回的相反布尔值 | 5 !== 6 // true |
若要使用运算符,可以在赋值或比较过程中将其用于 if/else 构造。
如果条件为 true,则 if 语句将运行块之间的代码。 if 子句中的条件可以是包含真值的值,或者是计算结果为 true 的条件语句。 以下代码演示如何表示 if 子句。
- if (condition){
- //Condition was true. Code in this block will run.
- }
可以将变量或布尔值传递给 if 子句,如下所示:
- let isTrue = true;
- if (isTrue) {
- // run code if true
- }
-
- if (true) {
- // run code if true
- }
逻辑运算符通常用于形成条件。 需要以下三项:值、显示所需比较方式的比较运算符以及要比较的值。 语法类似于以下代码:
- if (value operator compareToThisValue) {
- // run code
- }
当 if 条件的计算结果为 false 时,else 语句将运行块之间的代码。 使用 else 是可选操作。
- let currentMoney = 1000;
- let laptopPrice = 1500;
-
- if (currentMoney >= laptopPrice) {
- //Condition was true. Code in this block will run.
- console.log("Getting a new laptop!");
- } else{
- //Condition was true. Code in this block will run.
- console.log("Can't afford a new laptop, yet!");
- }
决策可能需要多个比较,并且可以与逻辑运算符串联在一起,以生成布尔值。
使用特定的运算符可以连接许多布尔语句。 结果是更复杂的语句,其计算结果仍为 true 或 false。 可以使用以下一组运算符来构造这些更复杂的比较:
| 符号 | 说明 | 示例 |
|---|---|---|
&& | 逻辑与:比较两个布尔表达式。 如果两侧都为 true,则仅返回 true。 | (5 > 6) && (5 < 6 ) //One side is false, other is true. Returns false |
|| | 逻辑或:比较两个布尔表达式。 如果至少一侧为 true,则返回 true。 | (5 > 6) || (5 < 6) //One side is false, other is true. Returns true |
! | 逻辑非:返回布尔表达式的相反值。 | !(5 > 6) // 5 is not greater than 6, but "!" will return true |
可以将这些复杂运算符与两个赋值一起使用,也可以与 if 和 else 子句一起使用。
在向变量赋值时,可以使用或 (||) 运算符。
- let isHoliday = true;
- let isMember = true;
- let hasDiscount = isHoliday || isMember;
在 if/else 语句中使用此类逻辑运算符。
通过使用 || 运算符,可以构造一个看起来更复杂的 if 子句。
- let currentMoney= 800;
- let laptopPrice = 1000;
- let laptopDiscountPrice = laptopPrice - (laptopPrice * .20) //Laptop price at 20 percent off
-
- if (currentMoney >= laptopPrice || currentMoney >= laptopDiscountPrice){
- //Condition was true. Code in this block will run.
- console.log("Getting a new laptop!");
- }
- else {
- //Condition was true. Code in this block will run.
- console.log("Can't afford a new laptop, yet!");
- }
涉及 if 的所有内容的计算结果都需要为 true 或 false。 通过使用 ! 运算符,可以对表达式求反。 它应如下所示:
- if (!condition) {
- // runs if condition is false
- } else {
- // runs if condition is true
- }
使用 if...else 并不是表示决策逻辑的唯一方法。 还可以使用名为三元运算符的方法。 其语法如下所示:
let variable = condition ? :
作为休闲游戏的一名初级游戏开发人员,你的任务是开始开发二十一点游戏。 对于如何计算点数,二十一点有一些有趣的规则。 你意识到这是应用所学的一些布尔逻辑的绝佳机会。
在二十一点中,游戏的目标是战胜庄家。 持有高于庄家但小于或等于 21 的点数即为获胜。
创建文件 blackjack.js。
- touch app.js
- code .
为该文件提供以下起始代码:
- let cardOne = 7;
- let cardTwo = 5;
- let sum = cardOne + cardTwo; // 15
接下来,你将添加用于模拟再发一张牌的代码。 让我们看看所发生的情况。
添加以下代码:
- let cardThree = 7;
- sum += cardThree;
- if (sum > 21) {
- console.log('You lost');
- process.exit(1); // exit program
- }
- console.log(`You have ${sum} points`);
运行代码。
node blackjack.js
可以看到以下输出:
You have 19 points 你的对手是庄家。 记住规则。 如果你的点数比庄家高,或者庄家超过 21 点,则你会获胜。 让我们实现这些规则。
添加以下变量来表示庄家纸牌。
- let cardOneBank = 7;
- let cardTwoBank = 5;
- let cardThreeBank = 6;
- let cardFourBank = 4;
接下来,将以下代码添加到文件末尾。
- let bankSum = cardOneBank + cardTwoBank + cardThreeBank + cardFourBank;
- if (bankSum > 21 || (sum <= 21 && sum > bankSum)) {
- console.log('You win');
- } else {
- console.log('Bank wins');
- }
运行代码。
node blackjack.js
可以看到以下输出:
- You have 19 points
- You win
祝贺你! 你已使用布尔逻辑和运算符成功实现了二十一点中的一些游戏规则。
完整代码如下:
- let cardOne = 7;
- let cardTwo = 5;
- let sum = cardOne + cardTwo; // 15
- let cardOneBank = 7;
- let cardTwoBank = 5;
- let cardThreeBank = 6;
- let cardFourBank = 4;
-
- let cardThree = 7;
- sum += cardThree;
- if (sum > 21) {
- console.log('You lost');
- }
- console.log(`You have ${sum} points`);
-
- let bankSum = cardOneBank + cardTwoBank + cardThreeBank + cardFourBank;
-
- if (bankSum > 21 || (sum <= 21 && sum > bankSum)) {
- console.log('You win');
- process.exit(1); // exit program
- } else {
- console.log('Bank wins');
- }