C#一条if语句中的多个布尔
我有一个C#一条if语句中的多个布尔,c#,C#,我有一个if语句,当前使用bool“debug” 看起来是这样的: for (; ; ) { if (debug) { Console.WriteLine("Please type in a command"); cmd = Console.ReadLine(); p.Send(cmd); Console.WriteLine("Command successfully executed\n"); if
if
语句,当前使用bool“debug”
看起来是这样的:
for (; ; )
{
if (debug)
{
Console.WriteLine("Please type in a command");
cmd = Console.ReadLine();
p.Send(cmd);
Console.WriteLine("Command successfully executed\n");
if (cmd == "switch")
{
debug = !debug;
}
else if (cmd == "night")
{
p.Send(night);
}
else if (cmd == "fps 300")
{
p.Send(threefps);
}
else if (cmd == "fps 600")
{
p.Send(sixfps);
}
else if (cmd == "fps max")
{
p.Send(maxfps);
}
else if (cmd == "quality")
{
p.Send(quality);
}
else if (cmd == "hud")
{
p.Send(hud);
}
else if (cmd == "greenscreen")
{
p.Send(green);
}
else if (cmd == "regular")
{
p.Send(regular);
}
}
else
{
Console.WriteLine("Press enter to execute config");
cmd2 = Console.ReadLine();
if (cmd2 == "switch")
{
debug = !debug;
}
WebConfigReader conf = new WebConfigReader(url);
string[] tokens = Regex.Split(conf.ReadString(), @"\r?\n|\r");
foreach (string s in tokens)
{
p.Send(s);
}
}
}
我正在努力做到这一点,所以如果检查另一个名为“beta”的bool,就会有一个else。你建议我怎么做?我尝试过使用一个简单的if-else,但它就是不起作用。对于2个布尔值,有4种不同的状态。您可以使用:
if (debug && beta)
else if (debug && !beta)
else if (!debug && beta)
else
其他小注释:如果使用cmd,我会使用switch(cmd)语句。A
bool
要么是真的,要么是假的,所以唯一的方法是将一个与另一个进行比较,例如if((debug)&!(beta))
示例开关打开字符串与一组字符串比较完全相同。开关最初的设计是因为它是一种非常有效的计算小数值范围的低级方法。在这段代码中使用开关并没有真正的好处。见鬼,即使是压缩,因为它们都是一行语句,你只需去掉括号,它所占用的空间也会比switch结构少。我曾经读到编译器根据case语句的数量决定如何实现switch。If7或更小的If将构建If树,否则它将构建具有跳转地址或lambda的字典。计算散列比进行几次比较花费更多的时间。在顶部设置最频繁的条目可能会有所帮助。开关读起来更好。