C# 处理多个 IF 语句的最佳用途

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9757770/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 08:44:05  来源:igfitidea点击:

Best use for handling multiple IF statements

c#if-statement

提问by supergeek1982

I'm wondering the best way to go about this: In my scenario users select between 1 - 5 options, depending on their options it has to randomly pick between the three.

我想知道解决这个问题的最佳方法:在我的场景中,用户在 1 - 5 个选项之间进行选择,根据他们的选项,它必须在三个选项之间随机选择。

Say the user has Option 2 and 3 Picked, I could write an IF statement saying when it's picked do this etc, the problem is I don't want to write IF statements for all the possible combinations.

假设用户选择了选项 2 和 3,我可以编写一个 IF 语句,说明它何时被选中执行此操作等,问题是我不想为所有可能的组合编写 IF 语句。

Just wondering what's the easiest way about this?

只是想知道什么是最简单的方法?

I could go a Switch I guess, but again that would involve programming out up each individual combination.

我想我可以选择 Switch,但这又会涉及对每个单独的组合进行编程。

Thanks.

谢谢。

Edit: Code for a small example

编辑:一个小例子的代码

bool Option1 = false;
bool Option2 = false;
bool Option3 = false;

if (Option1 == true && Option2 == false && Option3 == false)
{
//Do Somthing
}
else if (Option1 == true && Option2 == true && Option3 == false)
{
//Do Somthing
}
else if (Option1 == true && Option2 == false && Option3 == true )
{
//Do Somthing
}

And so on..

等等..

回答by Tomas Andrle

It depends on what "// Do Somthing"actually does.

这取决于"// Do Somthing"实际做什么。

If there's some code duplication between the options then put it in a function and call that. Or separate the duplicated code into a standalone if ( Option1 ) { ... }block.

如果选项之间存在一些代码重复,则将其放入一个函数中并调用它。或者将重复的代码分成一个独立的if ( Option1 ) { ... }块。

As you mentioned, you can turn the valid combinations into an enum (5 bits is 32 values) and put it in a switch statement.

正如您所提到的,您可以将有效组合转换为枚举(5 位是 32 个值)并将其放入 switch 语句中。

回答by Mario The Spoon

I would go for nested if's. you can always group them in functions if it becomes too unreadable:

我会去嵌套如果。如果它变得太不可读,您可以随时将它们分组到函数中:

if( option1 )
{
   if ( option2 )
   {
      if ( option3 )
      {
         doThing2();
      }
      else
      {
         doThing1(); // no more alternatives
      }
   }
   else
   {
      doThing1(); // need to do the same as above
   }
}
else
{
  ... more or less the same as above
}

I think that is the way it was meant to be. And with nice editors it becomes easily readable...

我认为这就是它本来的样子。有了漂亮的编辑器,它变得容易阅读......

hth

MNario

纳里奥

回答by Matthew Strawbridge

Try to write your code so it's closely tied to the logic you're trying to model. Does the choice made for option1change the meaning of the other options, or are they all to be taken as a group?

尝试编写您的代码,使其与您尝试建模的逻辑紧密相关。所做的选择是否会option1改变其他选项的含义,还是将它们全部视为一个整体?

You might find it useful to introduce other flags (e.g. exactlyTwoOptionsSelected) that you can then use in the branching.

您可能会发现引入exactlyTwoOptionsSelected可以在分支中使用的其他标志(例如)很有用。

By the way, writing if (option1 && option2)or if (!option1 || option2)etc. is going to be easier to work with than if (option1 == true && option2 == true)and if(option1 == false || option2 == true).

顺便说一句,写if (option1 && option2)if (!option1 || option2)等将是更容易的工作比if (option1 == true && option2 == true)if(option1 == false || option2 == true)

回答by Liviya Grigorova

        Console.WriteLine("Enter first number: ");
        int first = int.Parse(Console.ReadLine());
        Console.WriteLine("Enter second number: ");
        int second = int.Parse(Console.ReadLine());
        if (first % second == 0) 
        {
            Console.WriteLine("Number 1 is multiple of number 2.");
        }
        else if (second % first == 0)
        {
            Console.WriteLine("Number 2 is multiple of number 1.");
        }
        else
        {
            Console.WriteLine("Numbers are not multiples.");
        }
        Console.ReadKey();