• 2022年6月29日--使用C#迈出第一步--使用 C# 中的“if”、“else”和“else if”语句向代码添加决策逻辑


    了解如何通过评估布尔表达式来为代码的执行路径创建分支。

    简介

    通过 C# 编程语言,你可以构建采用决策逻辑的应用程序。 通过分支逻辑,你的应用程序可根据一组条件执行不同的指令。

    使用if语句

    运用最广泛的分支语句是 if 语句。 if 语句依赖布尔表达式,后者用一组括号括起来。 如果表达式为 true,则执行 if 语句后面的代码。 如果表达式为 false,则跳过 if 语句后面的代码。

    使用if语句创建随机数游戏

    我们将使用 Random.Next() 方法来模拟抛掷三个六面骰子。

    • 如果抛掷任何两个骰子得到相同的值,则因两个骰子投出同样的点数获得两点奖励得分。
    • 如果抛掷全部三个骰子都得到相同的值,则因三个骰子投出同样的点数获得六点奖励得分。
    • 如果三个骰子投出的点数之和加上所有奖励点数大于等于 15,则你赢得比赛。 否则,就是你输了。

    步骤1-编写代码以生成三个随机数字并在输出中显示它们

    1. Random dice = new Random();
    2. int roll1 = dice.Next(1, 7);
    3. int roll2 = dice.Next(1, 7);
    4. int roll3 = dice.Next(1, 7);
    5. int total = roll1 + roll2 + roll3;
    6. Console.WriteLine($"Dice roll: {roll1} + {roll2} + {roll3} = {total}");

    Dice roll: 2 + 2 + 2 = 6

    步骤2-添加一个if语句,以基于total变量的值显示不同的消息

    1. Random dice = new Random();
    2. int roll1 = dice.Next(1, 7);
    3. int roll2 = dice.Next(1, 7);
    4. int roll3 = dice.Next(1, 7);
    5. int total = roll1 + roll2 + roll3;
    6. Console.WriteLine($"Dice roll: {roll1} + {roll2} + {roll3} = {total}");
    7. if (total > 14)
    8. {
    9. Console.WriteLine("You win!");
    10. }
    11. if (total < 15)
    12. {
    13. Console.WriteLine("Sorry, you lose.");
    14. }

    1. Dice roll: 5 + 5 + 3 = 13
    2. Sorry, you lose.

    什么是布尔表达式?

    布尔表达式是返回布尔值(true 或 false)的任意代码。

    string.Contains() 方法计算某个字符串是否包含另一个字符串

    1. string message = "The quick brown fox jumps over the lazy dog.";
    2. bool result = message.Contains("dog");
    3. Console.WriteLine(result);
    4. if (message.Contains("fox"))
    5. {
    6. Console.WriteLine("What does the fox say?");
    7. }

    1. True
    2. What does the fox say?

    什么是代码块?

    步骤3-再添加一个if语句来实现双倍奖励

    1. Random dice = new Random();
    2. int roll1 = dice.Next(1, 7);
    3. int roll2 = dice.Next(1, 7);
    4. int roll3 = dice.Next(1, 7);
    5. int total = roll1 + roll2 + roll3;
    6. Console.WriteLine($"Dice roll: {roll1} + {roll2} + {roll3} = {total}");
    7. if ((roll1 == roll2) || (roll2 == roll3) || (roll1 == roll3))
    8. {
    9. Console.WriteLine("You rolled doubles! +2 bonus to total!");
    10. total += 2;
    11. }
    12. if (total >= 15)
    13. {
    14. Console.WriteLine("You win!");
    15. }
    16. if (total < 15)
    17. {
    18. Console.WriteLine("Sorry, you lose.");
    19. }

    1. Dice roll: 3 + 6 + 5 = 14
    2. Sorry, you lose.

    步骤4-再添加一个if语句来实现三倍奖励

    1. Random dice = new Random();
    2. int roll1 = dice.Next(1, 7);
    3. int roll2 = dice.Next(1, 7);
    4. int roll3 = dice.Next(1, 7);
    5. int total = roll1 + roll2 + roll3;
    6. Console.WriteLine($"Dice roll: {roll1} + {roll2} + {roll3} = {total}");
    7. if ((roll1 == roll2) || (roll2 == roll3) || (roll1 == roll3))
    8. {
    9. Console.WriteLine("You rolled doubles! +2 bonus to total!");
    10. total += 2;
    11. }
    12. if ((roll1 == roll2) && (roll2 == roll3))
    13. {
    14. Console.WriteLine("You rolled triples! +6 bonus to total!");
    15. total += 6;
    16. }
    17. if (total >= 15)
    18. {
    19. Console.WriteLine("You win!");
    20. }
    21. if (total < 15)
    22. {
    23. Console.WriteLine("Sorry, you lose.");
    24. }

    1. Dice roll: 1 + 1 + 5 = 7
    2. You rolled doubles! +2 bonus to total!
    3. Sorry, you lose.

    逻辑上的问题以及改进代码的机会

    回顾

    使用“else”和“else if”语句

    通过添加else和else if语句来改进代码并修复bug

    1. Random dice = new Random();
    2. int roll1 = dice.Next(1, 7);
    3. int roll2 = dice.Next(1, 7);
    4. int roll3 = dice.Next(1, 7);
    5. int total = roll1 + roll2 + roll3;
    6. Console.WriteLine($"Dice roll: {roll1} + {roll2} + {roll3} = {total}");
    7. if ((roll1 == roll2) || (roll2 == roll3) || (roll1 == roll3))
    8. {
    9. Console.WriteLine("You rolled doubles! +2 bonus to total!");
    10. total += 2;
    11. }
    12. if ((roll1 == roll2) && (roll2 == roll3))
    13. {
    14. Console.WriteLine("You rolled triples! +6 bonus to total!");
    15. total += 6;
    16. }
    17. if (total >= 15)
    18. {
    19. Console.WriteLine("You win!");
    20. }
    21. else
    22. {
    23. Console.WriteLine("Sorry, you lose.");
    24. }

    1. Dice roll: 5 + 1 + 4 = 10
    2. Sorry, you lose.

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

    1. int roll1 = 6;
    2. int roll2 = 6;
    3. int roll3 = 6;
    4. int total = roll1 + roll2 + roll3;
    5. Console.WriteLine($"Dice roll: {roll1} + {roll2} + {roll3} = {total}");
    6. if ((roll1 == roll2) || (roll2 == roll3) || (roll1 == roll3))
    7. {
    8. if ((roll1 == roll2) && (roll2 == roll3))
    9. {
    10. Console.WriteLine("You rolled triples! +6 bonus to total!");
    11. total += 6;
    12. }
    13. else
    14. {
    15. Console.WriteLine("You rolled doubles! +2 bonus to total!");
    16. total += 2;
    17. }
    18. }
    19. if (total >= 15)
    20. {
    21. Console.WriteLine("You win!");
    22. }
    23. else
    24. {
    25. Console.WriteLine("Sorry, you lose.");
    26. }

    1. Dice roll: 6 + 6 + 6 = 18
    2. You rolled triples! +6 bonus to total!
    3. You win!

    步骤2-使用嵌套消除双倍奖励和三倍奖励叠加的情形

     玩家应该只赢取一个奖品:

    • 如果玩家的分数大于等于 16,则赢得一辆新车。
    • 如果玩家的分数大于等于 10,则赢得一台新的笔记本电脑。
    • 如果玩家的分数正好为 7,则赢得一次旅行机会。
    • 否则,玩家赢得一只小猫。

    1. Random dice = new Random();
    2. int roll1 = dice.Next(1, 7);
    3. int roll2 = dice.Next(1, 7);
    4. int roll3 = dice.Next(1, 7);
    5. int total = roll1 + roll2 + roll3;
    6. Console.WriteLine($"Dice roll: {roll1} + {roll2} + {roll3} = {total}");
    7. if ((roll1 == roll2) || (roll2 == roll3) || (roll1 == roll3))
    8. {
    9. if ((roll1 == roll2) && (roll2 == roll3))
    10. {
    11. Console.WriteLine("You rolled triples! +6 bonus to total!");
    12. total += 6;
    13. }
    14. else
    15. {
    16. Console.WriteLine("You rolled doubles! +2 bonus to total!");
    17. total += 2;
    18. }
    19. }
    20. if (total >= 16)
    21. {
    22. Console.WriteLine("You win a new car!");
    23. }
    24. else if (total >= 10)
    25. {
    26. Console.WriteLine("You win a new laptop!");
    27. }
    28. else if (total == 7)
    29. {
    30. Console.WriteLine("You win a trip for two!");
    31. }
    32. else
    33. {
    34. Console.WriteLine("You win a kitten!");
    35. }

    1. Dice roll: 1 + 5 + 1 = 7
    2. You rolled doubles! +2 bonus to total!
    3. You win a kitten!

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

     玩家应该只赢取一个奖品:

    • 如果玩家的分数大于等于 16,则赢得一辆新车。
    • 如果玩家的分数大于等于 10,则赢得一台新的笔记本电脑。
    • 如果玩家的分数正好为 7,则赢得一次旅行机会。
    • 否则,玩家赢得一只小猫。

    1. Random dice = new Random();
    2. int roll1 = dice.Next(1, 7);
    3. int roll2 = dice.Next(1, 7);
    4. int roll3 = dice.Next(1, 7);
    5. int total = roll1 + roll2 + roll3;
    6. Console.WriteLine($"Dice roll: {roll1} + {roll2} + {roll3} = {total}");
    7. if ((roll1 == roll2) || (roll2 == roll3) || (roll1 == roll3))
    8. {
    9. if ((roll1 == roll2) && (roll2 == roll3))
    10. {
    11. Console.WriteLine("You rolled triples! +6 bonus to total!");
    12. total += 6;
    13. }
    14. else
    15. {
    16. Console.WriteLine("You rolled doubles! +2 bonus to total!");
    17. total += 2;
    18. }
    19. }
    20. if (total >= 16)
    21. {
    22. Console.WriteLine("You win a new car!");
    23. }
    24. else if (total >= 10)
    25. {
    26. Console.WriteLine("You win a new laptop!");
    27. }
    28. else if (total == 7)
    29. {
    30. Console.WriteLine("You win a trip for two!");
    31. }
    32. else
    33. {
    34. Console.WriteLine("You win a kitten!");
    35. }

    回顾

    挑战

    1. Random random = new Random();
    2. int daysUntilExpiration = random.Next(12);
    3. int discountPercentage = 0;
    4. // Your code goes here
    5. Console.WriteLine(daysUntilExpiration);
    6. if(daysUntilExpiration==1)
    7. {
    8. discountPercentage=20;
    9. Console.WriteLine("Your subscription expires within a day!");
    10. Console.WriteLine("Renew now and save {0}%!",discountPercentage);
    11. }
    12. else if(daysUntilExpiration>1&&daysUntilExpiration<=5)
    13. {
    14. discountPercentage=10;
    15. Console.WriteLine("Your subscription expires in {0} days.",daysUntilExpiration);
    16. Console.WriteLine("Renew now and save {0}%!",discountPercentage);
    17. }
    18. else if(daysUntilExpiration<=10)
    19. {
    20. Console.WriteLine("Your subscription will expire soon. Renew now!");
    21. }
    22. else
    23. {
    24. Console.WriteLine("Your subscription has expired.");
    25. }

    1. 9
    2. Your subscription will expire soon. Renew now!

    1. 11
    2. Your subscription has expired.

    1. 5
    2. Your subscription expires in 5 days.
    3. Renew now and save 10%!

    解决方案:

    1. Random random = new Random();
    2. int daysUntilExpiration = random.Next(12);
    3. int discountPercentage = 0;
    4. if (daysUntilExpiration == 0)
    5. {
    6. Console.WriteLine("Your subscription has expired.");
    7. }
    8. else if (daysUntilExpiration == 1)
    9. {
    10. Console.WriteLine("Your subscription expires within a day!");
    11. discountPercentage = 20;
    12. }
    13. else if (daysUntilExpiration <= 5)
    14. {
    15. Console.WriteLine($"Your subscription expires in {daysUntilExpiration} days.");
    16. discountPercentage = 10;
    17. }
    18. else if (daysUntilExpiration <= 10)
    19. {
    20. Console.WriteLine("Your subscription will expire soon. Renew now!");
    21. }
    22. if (discountPercentage > 0)
    23. {
    24. Console.WriteLine($"Renew now and save {discountPercentage}%.");
    25. }

  • 相关阅读:
    Leetcode周赛371补题(3 / 3)
    MYSQL SEQUENCE方案
    阿里云OSS前端直传+net core后端签名
    ARM64常见指令
    数位dp总结
    阿里、百度、美团 Java 面试题大合集,我来教你怎么顺利拿到 offer
    章节三:RASA Domain介绍
    Vagrant的安装和使用(附带安装Centos 7教程)
    【计算机方向】通信、算法、自动化、机器人、电子电气、计算机工程、控制工程、计算机视觉~~~~~合集!!!
    c++操作mysql(详解)
  • 原文地址:https://blog.csdn.net/DXB2021/article/details/125528597