C# Switch 语句示例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15046553/
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-10 13:53:02  来源:igfitidea点击:

Example of Switch Statements

c#visual-studio-2012switch-statement

提问by user2085275

If the total of this textbox is: PostDiscountTextBox.Text = $500.00, how do we make Switch statements stating that if the Post-discount Cost amount is between 0.00 and 999.99, display a message box with the message of "This amount qualifies for 'A-100' frequent flier miles" and an "OK" button?

如果此文本框的总数为:PostDiscountTextBox.Text = $500.00,我们如何制作 Switch 语句,说明如果 Post-discount Cost 金额介于 0.00 和 999.99 之间,则显示消息框并显示“此金额符合 'A 的条件” -100' 飞行常客里程”和“确定”按钮?

Will someone provide an example of a switch statement?

有人会提供一个switch语句的例子吗?

I only have this so far, and I don't think it follows anything at all. Will someone guide me through this? Thank you.

到目前为止我只有这个,我认为它根本没有任何意义。有人会指导我完成这个吗?谢谢你。

        switch (PostDiscountCostTextBox.Text)
        {
            case (0.00 < && PostDiscountCostTextBox.Text <= 999.00)

Thank everyone who helped, but I am trying to figure out how to use a switch statement that evaluates Post-discount Cost based on a range of numeric values (nothing about an if statement). Yes, many cases will be put, and these will be my first two cases. Will someone be kind enough to provide me an example so that I could fill the rest of my cases in? I have lots. Thank you.

感谢所有帮助过的人,但我试图弄清楚如何使用 switch 语句,该语句根据一系列数值(与 if 语句无关)评估折扣后成本。是的,会有很多案例,这将是我的前两个案例。有人会好心为我提供一个例子,以便我可以填写其余的案例吗?我有很多。谢谢你。

If the Post-discount Cost amount is between 0.00 and 999.99, display a message box with the message of "This amount qualifies for 'A-100' frequent flier miles." and an "OK" button. No title bar text or icon should be used.

如果折扣后成本金额介于 0.00 和 999.99 之间,则显示一个消息框,其中包含“此金额符合 'A-100' 飞行常客里程资格”的消息。和“确定”按钮。不应使用标题栏文本或图标。

If the Post-discount Cost amount is between 1,000.00 and 1,499.99, display a message box with the message of "This amount qualifies for 'B-500' frequent flier miles." and an "OK" button. No title bar text or icon should be used.

如果折扣后成本金额介于 1,000.00 和 1,499.99 之间,则显示带有消息的消息框“此金额符合‘B-500’飞行常客里程资格。” 和“确定”按钮。不应使用标题栏文本或图标。

回答by wRAR

You cannot check for ranges in switch, you should use a chain of else if. See Is using decimal ranges in a switch impossible in C#?

您不能检查 中的范围switch,您应该使用else if. 请参阅在 C# 中不可能在开关中使用小数范围?

回答by MarcinJuraszek

That kind of switchusage is not allowed in C#.

switchC# 中不允许这种用法。

Here is an example of proper switchusage

这是正确switch使用的示例

switch(n)       
{         
   case 1:   
      cost += 25;
      break;                  
   case 2:            
      cost += 25;
      break;           
   case 3:            
      cost += 50;
      break;         
   default:            
      Console.WriteLine("Invalid selection. Please select 1, 2, or 3.");            
      break;      
 }

Your example should be transform into if-elseif-elsestatement:

您的示例应该转换为if-elseif-else语句:

if(first_case_predicate)
{

}
else if(another_predicate)
{

}
else
{
    // like 'default' of switch
}

回答by MuhammadHani

The straightforward way is to use if elsestatements as the ranges check are not allowed for the switchoperation. The another tricky way is to use Dictionaries. The below code snippet is a demonstration of what you want using the second approach.

直接的方法是使用if else语句,因为switch操作不允许范围检查。另一种棘手的方法是使用Dictionaries. 下面的代码片段演示了使用第二种方法所需的内容。

decimal myVal = decimal.Parse(PostDiscountCostTextBox.Text);

        var conditions = new Dictionary<Func<int, bool>, Action>
        { 
            { x => x > 0 && x <= 999 ,    () => Console.WriteLine("This amount qualifies for 'A-100' frequent flier miles.")   } ,
            { x => x > 999 ,   () => Console.WriteLine("Dummy!")  } ,
        };

        cases.First(kvp => kvp.Key(myNum)).Value();

Not so easy as the if elseapproach, but desreves a test.

不像方法那么容易if else,但需要测试。