Can some one help me solve this logic using C#

Below is the logic for what I am trying to do .Can some one help me solve this using C#.

 string strMessage=string.empty;
 for (int i = 0; i < 20; ++i)
 {
     switch i
        {
        Case 1,2,7,5:
          strMessage="You Won";
    break;
        Case 6,8,10,3:
          strMessage="You can try again";
    break;

         }

   }
Response.write(strMessage);

Whenever the value of i is 1,2,7 or 5 strMessage="You won" Whenever the value of i is 6,8,10 or 3 strMessage="You can try again"


you could go with something like

public string Evaluate(int value)
{            
    if (new[] {1, 2, 7, 5}.Contains(value)) return "You Won";
    return new[] {3, 6, 8, 10 }.Contains(value) ? "Try again" : "";
}

not sure what you are trying to do with your loop... looks a bit broken, but if you are trying to write out for each 0..19

Enumerable.Range(0, 20).Select(Evaluate).ToList().ForEach(Response.write);

string strMessage = string.Empty;

for (int i = 0; i < 20; ++i) 
{
    switch(i)
    {
        case 1:
        case 2:
        case 7:
        case 5:
            strMessage = "You Won";
            break;
        case 6:
        case 8:
        case 10:
        case 3:
            strMessage = "You can try again";
        break;
    }
}

Response.write(strMessage);

This particular code will always result in

strMessage = "You can try again";

because of the for loop and the fact that when i>10 the switch will not do anything.

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

上一篇: 没有“break”的开关/外壳,不能正确检查外壳

下一篇: 有人可以帮助我解决这个逻辑使用C#