当前位置:网站首页>June 29, 2022 -- take the first step with C # -- add decision logic to the code using the "if", "else" and "else if" statements in C #
June 29, 2022 -- take the first step with C # -- add decision logic to the code using the "if", "else" and "else if" statements in C #
2022-06-30 07:02:00 【DXB2021】
Learn how to branch the execution path of your code by evaluating Boolean expressions .
brief introduction
adopt C# programing language , You can build applications that use decision logic . By branching logic , Your application can execute different instructions according to a set of conditions .
Use if sentence
The most widely used branch statements are if sentence . if Statement is a Boolean expression , The latter is enclosed in a set of parentheses . If the expression is true, execute if Code after statement . If the expression is false, Then skip if Code after statement .
Use if Statement to create a random number game
We will use Random.Next() Method to simulate throwing three six sided dice .
- If you roll any two dice to get the same value , You will get two bonus points for the same number of points on two dice .
- If you roll all three dice to get the same value , You will get six bonus points for three dice with the same number of points .
- If the sum of the three dice plus all bonus points is greater than or equal to 15, You win the game . otherwise , You lost .
step 1- Write code to generate three random numbers and display them in the output
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 = 6step 2- Add one if sentence , Based on total The value of the variable displays different messages
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.What is a Boolean expression ?
Boolean expressions return Boolean values (true or false) Arbitrary code for .
string.Contains() Method to calculate whether a string contains another string
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?What is a code block ?
step 3- Add another if Statement to achieve double reward
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.step 4- Add another if Statement to achieve triple reward
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.Logical problems and opportunities to improve code
review
Use “else” and “else if” sentence
By adding else and else if Statement to improve the code and fix 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.step 1- Use if and else sentence , Instead of two separate if sentence
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!step 2- Use nesting to eliminate the superposition of double rewards and triple rewards
Players should only win one prize :
- If the player's score is greater than or equal to 16, Win a new car .
- If the player's score is greater than or equal to 10, Win a new laptop .
- If the player's score is just 7, To win a chance to travel .
- otherwise , The player wins 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!");
}Dice roll: 1 + 5 + 1 = 7
You rolled doubles! +2 bonus to total!
You win a kitten!step 3- Use if、else and else if Statements provide rewards , Instead of showing a victory or defeat message
Players should only win one prize :
- If the player's score is greater than or equal to 16, Win a new car .
- If the player's score is greater than or equal to 10, Win a new laptop .
- If the player's score is just 7, To win a chance to travel .
- otherwise , The player wins 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!");
}review
Challenge
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%!Solution :
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}%.");
}边栏推荐
- 2022年6月29日--使用C#迈出第一步--使用 C# 中的“if”、“else”和“else if”语句向代码添加决策逻辑
- 【docsify基本使用】
- Out of class implementation of member function of class template
- First line of code (Third Edition) learning notes
- SOC project AHB_ SD_ Host controller design
- [Hot100]回文子串 与 最长回文子串
- Google Earth Engine(GEE)——墨累全球潮汐湿地变化 v1 (1999-2019) 数据集
- Linux server installation redis
- RT thread Kernel Implementation (II): critical area, object container
- leetcode:98. 验证二叉搜索树
猜你喜欢

SOC_SD_CLK

SOC_ SD_ CLK

Class template case - encapsulation of array classes

RT thread Kernel Implementation (V): timer

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

RT thread migration to s5p4418 (I): scheduler
![[mask RCNN] target detection and recognition based on mask RCNN](/img/80/f3db990b4f242609679d872b0dfe00.png)
[mask RCNN] target detection and recognition based on mask RCNN

Porting RT thread to s5p4418 (II): dynamic memory management

Principle: webmvcconfigurer and webmvcconfigurationsupport pit avoidance Guide

相关数据库问题提问。
随机推荐
QT signal slot alarm QObject:: connect:cannot connect (null)
Steps for formulating class or file templates in idea
ftplib+ tqdm 上传下载进度条
The most complete sentence in history
Linu基础-分区规划与使用
The solution of memcpy memory overlap
IDEA import导入的类明明存在,却飘红?
Win10踩坑-开机0xc0000225
经纬恒润再次荣获PACCAR集团 10PPM 质量奖
Why does ETL often become ELT or even let?
Fastapi learning Day2
Numpy中的四个小技巧
我开户后把账号忘记了咋办?股票在网上开户安全吗?
JS widget wave JS implementation of wave progress bar animation style
Imxq Freescale yocto project compilation record
freemarker
[Hot100]10. 正则表达式匹配
Four tips in numpy
Record common problems: spaces in encodeuricomponent decoding and the use of Schema in third-party apps to invoke apps
[semidrive source code analysis] [x9 chip startup process] 34 - RTOS side display module SDM_ display_ Init display initialization source code analysis