练习 - 使用 if、else if 和 else 创建嵌套决策逻辑

已完成

在上一单元中,你使用了多个 if 语句来实现游戏规则。 但在该单元结束时,你注意到需使用含义更明确的 if 语句才能修复代码中的细微 bug。

在本练习中,你将使用 ifelseelse if 语句来优化代码中的分支选项并修复逻辑 bug。

使用 if 和 else 语句,而不是两个单独的 if 语句

不是执行两项检查来显示消息“你获胜了!”或“抱歉,你失败了”,而是将使用 else 关键字。

  1. 确保 Program.cs 代码与以下内容匹配:

    Random dice = new Random();
    
    int roll1 = dice.Next(1, 7);
    int roll2 = dice.Next(1, 7);
    int roll3 = dice.Next(1, 7);
    
    int total = roll1 + roll2 + roll3;
    
    Console.WriteLine($"Dice roll: {roll1} + {roll2} + {roll3} = {total}");
    
    if ((roll1 == roll2) || (roll2 == roll3) || (roll1 == roll3))
    {
        Console.WriteLine("You rolled doubles! +2 bonus to total!");
        total += 2;
    }
    
    if ((roll1 == roll2) && (roll2 == roll3)) 
    {
        Console.WriteLine("You rolled triples! +6 bonus to total!");
        total += 6;
    }
    
    if (total >= 15)
    {
        Console.WriteLine("You win!");
    }
    
    if (total < 15)
    {
        Console.WriteLine("Sorry, you lose.");
    }
    
    

    这是你在上一单元中执行的代码。

  2. 花点时间检查文件末尾的两个 if 语句:

    if (total >= 15)
    {
        Console.WriteLine("You win!");
    }
    
    if (total < 15)
    {
        Console.WriteLine("Sorry, you lose.");
    }
    
    

    请注意,两个 if 语句都将 total 与同一数值进行比较。 这是使用 else 语句的绝佳机会。

  3. 更新两个 if 语句,如下所示:

    if (total >= 15)
    {
        Console.WriteLine("You win!");
    }
    else 
    {
        Console.WriteLine("Sorry, you lose.");
    }
    
    

    此处,如果 total >= 15 为 false,则将执行 else 关键字后面的代码块。 由于这两个结果是相对的,因此是适合 else 关键字的最佳场景。

  4. 更新后的 Program.cs 文件应包含以下代码:

    Random dice = new Random();
    
    int roll1 = dice.Next(1, 7);
    int roll2 = dice.Next(1, 7);
    int roll3 = dice.Next(1, 7);
    
    int total = roll1 + roll2 + roll3;
    
    Console.WriteLine($"Dice roll: {roll1} + {roll2} + {roll3} = {total}");
    
    if ((roll1 == roll2) || (roll2 == roll3) || (roll1 == roll3))
    {
        Console.WriteLine("You rolled doubles!  +2 bonus to total!");
        total += 2;
    }
    
    if ((roll1 == roll2) && (roll2 == roll3))
    {
        Console.WriteLine("You rolled triples!  +6 bonus to total!");
        total += 6;
    }
    
    if (total >= 15)
    {
        Console.WriteLine("You win!");
    }
    else 
    {
        Console.WriteLine("Sorry, you lose.");
    }
    
    

修改代码,通过嵌套消除双倍奖励和三倍奖励叠加的情形

在上一单元中,你发现应用程序中引入了一个细微的逻辑 bug。 你可以通过嵌套 if 语句来解决该问题。

通过嵌套,可以将代码块放在代码块中。 在此情况下,可以将 ifelse 组合(检查是否有双倍奖励)嵌套在另一个 if 语句(检查是否有三倍奖励)中,以防止同时颁发两种奖励。

  1. 修改代码以匹配以下代码清单:

    Random dice = new Random();
    
    int roll1 = dice.Next(1, 7);
    int roll2 = dice.Next(1, 7);
    int roll3 = dice.Next(1, 7);
    
    int total = roll1 + roll2 + roll3;
    
    Console.WriteLine($"Dice roll: {roll1} + {roll2} + {roll3} = {total}");
    
    if ((roll1 == roll2) || (roll2 == roll3) || (roll1 == roll3))
    {
        if ((roll1 == roll2) && (roll2 == roll3))
        {
            Console.WriteLine("You rolled triples!  +6 bonus to total!");
            total += 6;
        }
        else
        {
            Console.WriteLine("You rolled doubles!  +2 bonus to total!");
            total += 2;
        }
    }
    
    if (total >= 15)
    {
        Console.WriteLine("You win!");
    }
    else 
    {
        Console.WriteLine("Sorry, you lose.");
    }
    
    
  2. 花点时间查看嵌套的 if 语句。

    目标是创建一个内部 if-else 构造,其中两个结果是相对的,然后使用相对的结果(if/true 和 else/false)颁发三倍奖励和双倍奖励的奖励积分。 为实现此目标,在外部 if 语句中检查是否有双倍奖励,然后在内部 if 语句中检查是否有三倍奖励。 此模式确保检查是否有三倍奖励的内部语句返回 false 时,else 代码块可以颁发双倍奖励的积分。

    接下来,你将对三次掷骰结果进行“硬编码”,以测试代码逻辑。

  3. 在已声明和初始化 total 的行的上方创建一个空白代码行。

  4. 若要测试是否掷出双倍奖励,请输入以下代码:

    roll1 = 6;
    roll2 = 6;
    roll3 = 5;
    

    通过对三个 roll 变量进行硬编码,无需数十次运行应用程序即可测试代码。

  5. 在 Visual Studio Code 的“文件”菜单上,单击“保存”。

  6. 在“资源管理器”面板中,若要在 TestProject 文件夹位置打开终端,请右键单击“TestProject”,然后选择“在集成终端中打开”。

    终端面板应打开,并应包含一个命令提示符,显示终端已打开转到 TestProject 文件夹位置。

  7. 在终端命令提示符处,若要运行代码,请键入 dotnet run,然后按 Enter。

    代码运行时,你应看到:

    Dice roll: 6 + 6 + 5 = 17
    You rolled doubles!  +2 bonus to total!
    You win!
    
    
  8. 若要测试是否掷出三倍奖励,请更新硬编码的掷骰变量,如下所示:

    roll1 = 6;
    roll2 = 6;
    roll3 = 6;
    
  9. 在 Visual Studio Code 的“文件”菜单上,单击“保存”。

  10. 在“资源管理器”面板中,若要在 TestProject 文件夹位置打开终端,请右键单击“TestProject”,然后选择“在集成终端中打开”。

  11. 在终端命令提示符处,若要运行代码,请键入 dotnet run,然后按 Enter。

    代码运行时,你应看到:

    Dice roll: 6 + 6 + 6 = 18
    You rolled triples!  +6 bonus to total!
    You win!
    
    

使用 if、else 和 else if 语句提供奖励,而不是显示胜败消息

为了使游戏更有趣,可以将游戏从输赢结果改为对每个分数奖励虚拟奖品。 可以提供四个奖品。 但玩家应该只赢取一个奖品:

  • 如果玩家的分数大于等于 16,则赢得一辆新车。
  • 如果玩家的分数大于等于 10,则赢得一台新的笔记本电脑。
  • 如果玩家的分数正好为 7,则赢得一次旅行机会。
  • 否则,玩家赢得一只小猫。
  1. 针对以下代码清单修改前面步骤中的代码:

    Random dice = new Random();
    
    int roll1 = dice.Next(1, 7);
    int roll2 = dice.Next(1, 7);
    int roll3 = dice.Next(1, 7);
    
    int total = roll1 + roll2 + roll3;
    
    Console.WriteLine($"Dice roll: {roll1} + {roll2} + {roll3} = {total}");
    
    if ((roll1 == roll2) || (roll2 == roll3) || (roll1 == roll3))
    {
        if ((roll1 == roll2) && (roll2 == roll3))
        {
            Console.WriteLine("You rolled triples!  +6 bonus to total!");
            total += 6;
        }
        else
        {
            Console.WriteLine("You rolled doubles!  +2 bonus to total!");
            total += 2;
        }
    
        Console.WriteLine($"Your total including the bonus: {total}");
    }
    
    if (total >= 16)
    {
        Console.WriteLine("You win a new car!");
    }
    else if (total >= 10)
    {
        Console.WriteLine("You win a new laptop!");
    }
    else if (total == 7)
    {
        Console.WriteLine("You win a trip for two!");
    }
    else
    {
        Console.WriteLine("You win a kitten!");
    }
    
    
  2. 花点时间查看更新后的 if-elseif-else 构造。

    使用 ifelse ifelse 语句可以创建多个排他性条件作为布尔表达式。 换言之,如果你只希望产生一种结果,但却有多个可能的条件和结果,则根据需要使用任意数量的 else if 语句。 如果 ifelse if 语句均不适用,则将执行最后的 else 代码块。 else 是可选语句,但如果选择包含该语句,则该语句必须最后执行。

  3. 使用对 roll 变量进行临时硬编码方法来测试每条消息。

回顾

  • 通过使用 ifelse 语句的组合,可以测试一个条件,然后执行两个结果之一。 if 的代码块将在布尔表达式为 true 时运行,而 else 的代码块将在布尔表达式为 false 时运行。
  • 可以嵌套“if”语句来减少可能的条件。 但是,应考虑改用 ifelse ifelse 语句。
  • 使用 else if 语句创建多个排他性条件。
  • else 是可选语句,但包含该语句时该语句必须最后执行。