了解如何通过评估布尔表达式来为代码的执行路径创建分支。
通过 C# 编程语言,你可以构建采用决策逻辑的应用程序。 通过分支逻辑,你的应用程序可根据一组条件执行不同的指令。
运用最广泛的分支语句是 if
语句。 if
语句依赖布尔表达式,后者用一组括号括起来。 如果表达式为 true,则执行 if
语句后面的代码。 如果表达式为 false,则跳过 if
语句后面的代码。
我们将使用 Random.Next()
方法来模拟抛掷三个六面骰子。
- 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}");
Dice roll: 2 + 2 + 2 = 6
- 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 (total > 14)
- {
- Console.WriteLine("You win!");
- }
-
- if (total < 15)
- {
- Console.WriteLine("Sorry, you lose.");
- }
- Dice roll: 5 + 5 + 3 = 13
- Sorry, you lose.
布尔表达式是返回布尔值(true
或 false
)的任意代码。
string.Contains()
方法计算某个字符串是否包含另一个字符串
- string message = "The quick brown fox jumps over the lazy dog.";
- bool result = message.Contains("dog");
- Console.WriteLine(result);
-
- if (message.Contains("fox"))
- {
- Console.WriteLine("What does the fox say?");
- }
- True
- What does the fox say?
- 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 (total >= 15)
- {
- Console.WriteLine("You win!");
- }
-
- if (total < 15)
- {
- Console.WriteLine("Sorry, you lose.");
- }
- Dice roll: 3 + 6 + 5 = 14
- Sorry, you lose.
- 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.");
- }
- Dice roll: 1 + 1 + 5 = 7
- You rolled doubles! +2 bonus to total!
- Sorry, you lose.
- 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.");
- }
- Dice roll: 5 + 1 + 4 = 10
- Sorry, you lose.
- int roll1 = 6;
- int roll2 = 6;
- int roll3 = 6;
-
- 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.");
- }
- Dice roll: 6 + 6 + 6 = 18
- You rolled triples! +6 bonus to total!
- You win!
玩家应该只赢取一个奖品:
- 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 >= 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!");
- }
- Dice roll: 1 + 5 + 1 = 7
- You rolled doubles! +2 bonus to total!
- You win a kitten!
玩家应该只赢取一个奖品:
- 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 >= 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!");
- }
- Random random = new Random();
- int daysUntilExpiration = random.Next(12);
- int discountPercentage = 0;
-
- // Your code goes here
-
- Console.WriteLine(daysUntilExpiration);
- if(daysUntilExpiration==1)
- {
- discountPercentage=20;
- Console.WriteLine("Your subscription expires within a day!");
- Console.WriteLine("Renew now and save {0}%!",discountPercentage);
- }
- else if(daysUntilExpiration>1&&daysUntilExpiration<=5)
- {
- discountPercentage=10;
- Console.WriteLine("Your subscription expires in {0} days.",daysUntilExpiration);
- Console.WriteLine("Renew now and save {0}%!",discountPercentage);
- }
- else if(daysUntilExpiration<=10)
- {
- Console.WriteLine("Your subscription will expire soon. Renew now!");
- }
- else
- {
- Console.WriteLine("Your subscription has expired.");
- }
- 9
- Your subscription will expire soon. Renew now!
- 11
- Your subscription has expired.
- 5
- Your subscription expires in 5 days.
- Renew now and save 10%!
解决方案:
- Random random = new Random();
- int daysUntilExpiration = random.Next(12);
- int discountPercentage = 0;
-
- if (daysUntilExpiration == 0)
- {
- Console.WriteLine("Your subscription has expired.");
- }
- else if (daysUntilExpiration == 1)
- {
- Console.WriteLine("Your subscription expires within a day!");
- discountPercentage = 20;
- }
- else if (daysUntilExpiration <= 5)
- {
- Console.WriteLine($"Your subscription expires in {daysUntilExpiration} days.");
- discountPercentage = 10;
- }
- else if (daysUntilExpiration <= 10)
- {
- Console.WriteLine("Your subscription will expire soon. Renew now!");
- }
-
- if (discountPercentage > 0)
- {
- Console.WriteLine($"Renew now and save {discountPercentage}%.");
- }