理解 C# If-Else 语句:新手指南
在 C# 编程中,基于条件的决策是创建动态响应应用程序的基本方面。 这时就需要用到 if-else 语句了。 C# 中的 if-else 语句允许程序根据指定条件是真还是假来执行不同的代码块。
本指南的灵感来源于LaylaCodesIt的视频" C# 入门教程第 2 部分 - 学习如何编写 'if-else' 语句和 'while' 循环",它将向您介绍 C# 中 if-else 语句的基础知识,以及在编写布尔条件时使用条件运算符和逻辑运算符,并演示如何使您的程序能够智能地与用户输入进行交互。
引入条件逻辑
条件逻辑允许程序根据某些条件做出决策。 Layla 的视频介绍了 if-else 语句的概念和语法,这是一个条件逻辑的例子,它会根据指定条件是真还是假来执行不同的代码块。
在以下代码示例中,Layla 编写了一条问候消息,询问用户是否喜欢奶酪:
// Asking the user a question and storing their response
Console.WriteLine($"Hello {name}, do you like cheese?");
string doTheyLikeCheese = Console.ReadLine();// Asking the user a question and storing their response
Console.WriteLine($"Hello {name}, do you like cheese?");
string doTheyLikeCheese = Console.ReadLine();Simple If-Else Statement in C
然后,这个 if-else 条件语句会检查用户的回答,并根据用户的回答改变其输出响应。 该条件由条件运算符构成,并根据其结果执行 if-else 块中的语句。 以下是一个包含一行语句的简单示例:
// Checking the user's response and providing appropriate output
if (doTheyLikeCheese == "yes")
{
Console.WriteLine("Here is a lump of cheese.");
}
else
{
Console.WriteLine("Here is a lump of coal.");
}// Checking the user's response and providing appropriate output
if (doTheyLikeCheese == "yes")
{
Console.WriteLine("Here is a lump of cheese.");
}
else
{
Console.WriteLine("Here is a lump of coal.");
}如果用户回答"是",则 if 语句中的代码将显示一条消息,提供一块奶酪。 如果用户回复的不是"是",则 else 代码块将输出"这里有一块煤"。
区分大小写问题
使用基本的 if-else 语句时,可能会出现一个常见问题:区分大小写。 如果用户输入"Yes"而不是"yes",程序将无法识别为有效输入。 为了解决这个问题,Layla使用了ToLower()方法,该方法自动将用户的输入转换为小写,避免了大小写敏感性问题。
// Using ToLower() to handle case sensitivity in user input
if (doTheyLikeCheese.ToLower() == "yes")
{
Console.WriteLine("Here is a lump of cheese.");
}
else
{
Console.WriteLine("Here is a lump of coal.");
}// Using ToLower() to handle case sensitivity in user input
if (doTheyLikeCheese.ToLower() == "yes")
{
Console.WriteLine("Here is a lump of cheese.");
}
else
{
Console.WriteLine("Here is a lump of coal.");
}处理多种条件
Layla 接着解释了如何使用 else-if 语句处理两个以上可能的响应。 在这里,程序会检查多个条件语句——无论用户回答"是"、"否"还是其他任何内容。
// Checking multiple conditions using else-if
if (doTheyLikeCheese.ToLower() == "yes")
{
Console.WriteLine("Here is a lump of cheese.");
}
else if (doTheyLikeCheese.ToLower() == "no")
{
Console.WriteLine("Here is a lump of coal.");
}
else
{
Console.WriteLine("Please give a yes or no answer.");
}// Checking multiple conditions using else-if
if (doTheyLikeCheese.ToLower() == "yes")
{
Console.WriteLine("Here is a lump of cheese.");
}
else if (doTheyLikeCheese.ToLower() == "no")
{
Console.WriteLine("Here is a lump of coal.");
}
else
{
Console.WriteLine("Please give a yes or no answer.");
}和以前一样,如果 if 语句中的布尔表达式为真,则当用户回答"是"时,程序会提供一块奶酪。 然而,在 else-if 语句中,它不再对任何其他回复都提供一块煤炭,而是只在用户回复"否"时才提供。 如果用户回答的不是"是"或"否",则程序的 else 部分会要求用户提供"是"或"否"的答案。 让我们看看上述代码的实际运行结果:(图片可以改进)

使用循环
为了确保用户提供有效响应,引入了循环。 循环可以反复要求用户输入,直到用户提供正确答案为止。 这叫做"while"循环,下面的例子展示了它的工作原理:
// Using a while loop to repeatedly ask for valid input
bool hasAnswered = false;
while (!hasAnswered)
{
// Checking the user's response and setting hasAnswered to true for valid responses
if (doTheyLikeCheese.ToLower() == "yes")
{
Console.WriteLine("Here is a lump of cheese.");
hasAnswered = true;
}
else if (doTheyLikeCheese.ToLower() == "no")
{
Console.WriteLine("Here is a lump of coal.");
hasAnswered = true;
}
else
{
Console.WriteLine("Please give a yes or no answer.");
doTheyLikeCheese = Console.ReadLine();
}
}// Using a while loop to repeatedly ask for valid input
bool hasAnswered = false;
while (!hasAnswered)
{
// Checking the user's response and setting hasAnswered to true for valid responses
if (doTheyLikeCheese.ToLower() == "yes")
{
Console.WriteLine("Here is a lump of cheese.");
hasAnswered = true;
}
else if (doTheyLikeCheese.ToLower() == "no")
{
Console.WriteLine("Here is a lump of coal.");
hasAnswered = true;
}
else
{
Console.WriteLine("Please give a yes or no answer.");
doTheyLikeCheese = Console.ReadLine();
}
}在上面的例子中,循环会一直提示用户,直到他们回答"是"或"否"为止。 在莱拉的视频中,你可以看到代码执行时的实际效果:

If-Else语句 - 回顾
总结一下:
- if-else 语句可以在你的代码中做出决策。
- 您可以使用 else-if 语句处理多个条件。 循环确保用户做出正确响应。
条件逻辑是 C# 编程语言的基础,因此最好练习这些概念并开始将它们应用到你的代码中。 而这仅仅是个开始。 If 语句、else 语句、else-if 阶梯语句、if-else 嵌套语句(也称为嵌套 if-else)、switch 语句——条件逻辑可以极大地改变你的程序,而且有很多东西需要学习。
结论
瞧! 这是 if-else 语句和 while 循环的入门指南。 如前所述,本指南的灵感来源于 LaylaCodesIt - 请查看她的频道,了解上述所有内容的视频演示以及有关 C# 编程的更多信息。
与此同时,不妨看看我们的其他资源,她会在那里分享我们最喜欢的创作者、教程和学习资源,以提升您的 C# 技能。 祝您编码愉快!




