C# 如何根据用户输入在一行上声明多个if语句?
在上下文中,我希望用户在一行中使用两个if语句输入两个决策,这两个决策意味着相同的事情。有几种方法可以实现这一点。也许最简单的方法是使用C# 如何根据用户输入在一行上声明多个if语句?,c#,if-statement,C#,If Statement,在上下文中,我希望用户在一行中使用两个if语句输入两个决策,这两个决策意味着相同的事情。有几种方法可以实现这一点。也许最简单的方法是使用|运算符: if (userInput == "hello") ***or*** (userInput == "bye") { Console.WriteLine("So, which one is it?"); } 如果列表较长,可以创建一个列表,并查找如下匹配项: if (userInput == "hello" || userInput == "
|
运算符:
if (userInput == "hello") ***or*** (userInput == "bye")
{
Console.WriteLine("So, which one is it?");
}
如果列表较长,可以创建一个列表,并查找如下匹配项:
if (userInput == "hello" || userInput == "bye")
{
Console.WriteLine("So, which one is it?");
}
这很简单:
switch (userInput) {
case "hello":
case "bye":
...
break;
case "go-away":
case "come-back":
...
break;
}
您可以为或执行以下操作:
if (userInput == "hello" || userInput == "bye") // This line had changed
{
Console.WriteLine("So, which one is it?");
}
或者,如果你想要两者都是真实的
if (userInput == "hello" || userInput == "bye")
{
Console.WriteLine("So, which one is it?");
}
对于第二个选项,这不是一个很好的主意,因为它太长并且不能在所有浏览器中工作
if (userInput == "hello" || userInput == "bye")
{
Console.WriteLine("So, which one is it?");
}
if (userInput == "hello" && userInput == "bye")
{
Console.WriteLine("So, which one is it?");
}