当前位置:网站首页>2022年6月29日--使用C#迈出第一步--使用 C# 中的“if”、“else”和“else if”语句向代码添加决策逻辑
2022年6月29日--使用C#迈出第一步--使用 C# 中的“if”、“else”和“else if”语句向代码添加决策逻辑
2022-06-30 06:54:00 【DXB2021】
了解如何通过评估布尔表达式来为代码的执行路径创建分支。
简介
通过 C# 编程语言,你可以构建采用决策逻辑的应用程序。 通过分支逻辑,你的应用程序可根据一组条件执行不同的指令。
使用if语句
运用最广泛的分支语句是 if 语句。 if 语句依赖布尔表达式,后者用一组括号括起来。 如果表达式为 true,则执行 if 语句后面的代码。 如果表达式为 false,则跳过 if 语句后面的代码。
使用if语句创建随机数游戏
我们将使用 Random.Next() 方法来模拟抛掷三个六面骰子。
- 如果抛掷任何两个骰子得到相同的值,则因两个骰子投出同样的点数获得两点奖励得分。
- 如果抛掷全部三个骰子都得到相同的值,则因三个骰子投出同样的点数获得六点奖励得分。
- 如果三个骰子投出的点数之和加上所有奖励点数大于等于 15,则你赢得比赛。 否则,就是你输了。
步骤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}");Dice roll: 2 + 2 + 2 = 6步骤2-添加一个if语句,以基于total变量的值显示不同的消息
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?什么是代码块?
步骤3-再添加一个if语句来实现双倍奖励
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.步骤4-再添加一个if语句来实现三倍奖励
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.逻辑上的问题以及改进代码的机会
回顾
使用“else”和“else if”语句
通过添加else和else if语句来改进代码并修复bug
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.步骤1-使用if和else语句,而不是两个单独的if语句
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!步骤2-使用嵌套消除双倍奖励和三倍奖励叠加的情形
玩家应该只赢取一个奖品:
- 如果玩家的分数大于等于 16,则赢得一辆新车。
- 如果玩家的分数大于等于 10,则赢得一台新的笔记本电脑。
- 如果玩家的分数正好为 7,则赢得一次旅行机会。
- 否则,玩家赢得一只小猫。
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!步骤3-使用if、else和else if语句提供奖励,而不是显示胜败消息
玩家应该只赢取一个奖品:
- 如果玩家的分数大于等于 16,则赢得一辆新车。
- 如果玩家的分数大于等于 10,则赢得一台新的笔记本电脑。
- 如果玩家的分数正好为 7,则赢得一次旅行机会。
- 否则,玩家赢得一只小猫。
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}%.");
}边栏推荐
- [transfer] analysis of memory structure, cache and DMA architecture
- What underlying technologies support the metauniverse?
- Servlet principle
- Judge whether H5 is in wechat environment or enterprise wechat environment at both ends
- 【模糊神经网络】基于模糊神经网络的移动机器人路径规划
- 原来你是这样的数组,终于学会了
- Problems and solutions of creating topic messages in ROS
- SOC_ SD_ CLK
- Never forget the original intention, and be lazy if you can: C # operate word files
- leetcode:98. 验证二叉搜索树
猜你喜欢

Initial love with mqtt

Porting RT thread to s5p4418 (V): thread communication

Relevant database questions.
![[mask RCNN] target detection and recognition based on mask RCNN](/img/80/f3db990b4f242609679d872b0dfe00.png)
[mask RCNN] target detection and recognition based on mask RCNN

0基础转行软件测试,如何实现月薪9.5k+

第一行代码(第三版)学习笔记

Google Earth Engine(GEE)——墨累全球潮汐湿地变化 v1 (1999-2019) 数据集

【转】存储器结构、cache、DMA架构分析

Four tips in numpy

【我的OpenGL学习进阶之旅】关于OpenGL的着色器的向量和矩阵分类的访问方式: xyzw/rgba/stpq以及数组下标
随机推荐
RT thread migration to s5p4418 (III): static memory pool management
oracle
Notes: environment variables
Idea shortcut key
tomorrow! "Mobile cloud Cup" competition air publicity will start!
Go常用命令
1.4 - fixed and floating point numbers
ETL为什么经常变成ELT甚至LET?
【模糊神经网络】基于模糊神经网络的移动机器人路径规划
相关数据库问题提问。
【json-tutorial】第一章学习笔记
Record one time of Tencent Test Development Engineer's automation interface test practice experience
Rising posture series: fancy debugging information
SOC项目AHB_SD_HOST控制器设计
Basic questions (I)
1.8 - multi level storage
Go项目目录结构介绍
Never forget the original intention, and be lazy if you can: C # operate word files
6、 Shopping ⻋ and orders
How does the CPU recognize the code?