Case/switch statements c#?

I haven't a living clue how to write this in c#. I know how to do it in Delphi.

This is how I would do it in Delphi:

Case Total of
    80..100 : ShowMessage ('You got an A!');
    60..79  : ShowMessage ('You got a B!');
    50..59  : ShowMessage ('You got a C!');
    40..49  : ShowMessage ('You got a D!');
    0..39   : ShowMessage ('You got an E...');
  end;

I have read tutorials for this, but I wouldn't know how to use/write it in the way I need it.

Here's my version in c#:

switch (Total) //'Total' is a bunch of Integers divided together to get a Percentage
            {
             case 80..100: //Where 80% to 100% would achieve an A
            {
             MessageBox.Show("You got an A!");
            }
             case 60..79: //Where 60% to 79% would achieve a B
            {
             MessageBox.Show("You got a B!");
            }

Is this possible?

Thanks.


To express a range in a switch / case statement in C# you have to manually list out the cases

switch (Total) {
  case 80:
  case 81: 
  ...
  case 100:
    MessageBox.Show("you got an A");
    break;
  ...
}

For large ranges such as this though it may be better to just use a series of if statements

if (Total >= 80 && Total <= 100) { 
   MessageBox.Show("you got an A");
} else if (Total >= 70) { 
   MessageBox.Show("you got a B");
}

C# does not have a particularly flexible switch statement. This is intentional, options are limited to ensure that code generation is fast. C# has never been a language that hides execution cost. Runtime implementation is through a jump table, indexed by the switch() expression. Very fast, but not very flexible.

The alternative is an explicit if/else if chain. The exact same code that a VB.NET or Delphi compiler generates, but written by hand. Not terribly pretty, but effective enough:

if (Total < 0 || Total > 100) throw new ArgumentException("Not a valid grade");
if      (Total >= 80) ShowMessage ('You got an A!');
else if (Total >= 60) ShowMessage ('You got a B!');
else if (Total >= 50) ShowMessage ('You got a C!');
else if (Total >= 40) ShowMessage ('You got a D!');
else                  ShowMessage ('You got an E...');

This now also shows the cost associated with the code, there can be up to 6 comparisons on the Total value. If speed is essential and the range is limited then consider switching to a lookup table, a Dictionary<>. Not necessary here, ShowMessage() is an expensive method.


If it is supposed to be a switch then it should be like this:

switch(Total)
{
     case 100:
     case 99:
     //...
     case 80:
         MessageBox.Show("You got an A!");
         break;

     case 79:
     // ...
}

If it is okay to use if-statements I would recommend to do it like this:

if (Total < 0)
{
}
else if (Total < 40)
    MessageBox.Show("You got an E...")
else if (Total < 50)
    MessageBox.Show("You got a D!");
else if (Total < 60)
// etc.

Hop you could use this answer.

链接地址: http://www.djcxy.com/p/84454.html

上一篇: 如何处理一个案件中的多个值?

下一篇: case / switch语句c#?