Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Switch语句示例_C#_Visual Studio 2012_Switch Statement - Fatal编程技术网

C# Switch语句示例

C# Switch语句示例,c#,visual-studio-2012,switch-statement,C#,Visual Studio 2012,Switch Statement,如果此文本框的总数为:PostDiscountTextBox.Text=$500.00,我们如何做出转换声明,说明如果Post折扣成本金额在0.00和999.99之间,则显示一个消息框,消息框中显示“此金额符合‘a-100’飞行常客里程”和“确定”按钮 是否有人提供switch语句的示例? 到目前为止,我只知道这一点,我认为它根本没有任何意义。有人能引导我完成这件事吗?多谢各位 switch (PostDiscountCostTextBox.Text) {

如果此文本框的总数为:PostDiscountTextBox.Text=$500.00,我们如何做出转换声明,说明如果Post折扣成本金额在0.00和999.99之间,则显示一个消息框,消息框中显示“此金额符合‘a-100’飞行常客里程”和“确定”按钮

是否有人提供switch语句的示例?

到目前为止,我只知道这一点,我认为它根本没有任何意义。有人能引导我完成这件事吗?多谢各位

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

案例(0.00<&&PostDiscountCostTextBox.Text您不能在
开关中检查范围,您应该使用
链,否则如果
。请参阅在C中不允许使用这种用法

下面是一个正确使用
开关的示例

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;      
 }
您的示例应转换为
if elseif else
语句:

if(first_case_predicate)
{

}
else if(another_predicate)
{

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

简单的方法是使用
if-else
语句,因为
开关
操作不允许进行范围检查。另一种棘手的方法是使用
字典
。下面的代码片段演示了使用第二种方法所需的内容

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();
decimal myVal=decimal.Parse(PostDiscountCostTextBox.Text);
var条件=新字典
{ 
{x=>x>0&&x Console.WriteLine(“此金额符合'A-100'常旅客里程数要求)。),
{x=>x>999,()=>Console.WriteLine(“Dummy!”)},
};
cases.First(kvp=>kvp.Key(myNum)).Value();

不像
if-else
方法那么简单,但是它提供了一个测试。

如果你只有一个条件,你不需要
开关,你可以只使用一个
if
。可能的重复对我来说是有建设性的。我的第一个想法是利用三元操作数来提高效率。比如e> 双倍金额=折扣后成本文本框。文本;报告(0<金额