当前位置:网站首页>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}%.");
}边栏推荐
- Jingwei Hengrun won the 10ppm quality award of paccar group again
- [semidrive source code analysis] [x9 chip startup process] 33 - Analysis of related concepts of display module
- How to convert XML to JSON
- 2021-07-02
- Use of sscanf function
- 【申博攻略】五.专家推荐信模板
- 安装setup对应的组件
- Linux server installation redis
- 【SemiDrive源码分析】【X9芯片启动流程】34 - RTOS侧 Display模块 sdm_display_init 显示初始化源码分析
- 【已实现】服务器jar包启动脚本、shell脚本
猜你喜欢

Linux服務器安裝Redis
![[docsify basic use]](/img/9d/db689f5f13708f3e241474afeca1d0.png)
[docsify basic use]

随机网络,无标度网络,小世界网络以及NS小世界的性能对比matlab仿真

Jingwei Hengrun won the 10ppm quality award of paccar group again

【每日一题】535. TinyURL 的加密与解密

Why does ETL often become ELT or even let?

SOC project AHB_ SD_ Host controller design

相关数据库问题提问。

SOC_AHB_SD_IF

RT thread Kernel Implementation (V): timer
随机推荐
[transfer] analysis of memory structure, cache and DMA architecture
Win10踩坑-开机0xc0000225
Keil - the "trace HW not present" appears during download debugging
Why does ETL often become ELT or even let?
RT thread migration to s5p4418 (IV): thread synchronization
RT thread Kernel Implementation (IV): multi priority
JS null judgment operators | and? Usage of
Four great happenings on earth
SOC project AHB_ SD_ Host controller design
Browser downloads files as attachments
Joseph problem C language
Xshell传输文件
Porting RT thread to s5p4418 (II): dynamic memory management
Goland常用快捷键设置
sscanf 函数的使用
Traverse map
Deploying web projects using idea
Daemon and user threads
【Mask-RCNN】基于Mask-RCNN的目标检测和识别
Definition and use of ROS topic messages