0

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"

Lee
  • 665
  • 3
  • 7
  • 16

7 Answers7

7
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);
Marius Schulz
  • 15,976
  • 12
  • 63
  • 97
Galaxydreams
  • 318
  • 3
  • 13
2

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.

Emil Lundberg
  • 7,268
  • 6
  • 37
  • 53
1

your loop will cause all the messages to be printed, as well as 12 empty lines.

here is some psuedocode for what you have:

for the values 0 to 19 
    if i == 1 or 2 or 7 or 5 then message = "You Won"
    else if i == 3 or 6 or 8 or 10 then message = "You can try again"
    else  message = string.empty

if you walk through your code in your head, or use pencil and paper your problem will become clear.

so, when it hits the largest number in you case (its 10) the message will be set to "You can try again" and will not be set again.

so, when the loop ends the message will be "You can try again"... to fix this, either move your Response.Write to be inside the loop

the syntax on your switch is all wrong:

switch (i)
{
   case 1:
   case 2:
   // do something
   break;
}

here is the MSDN reference for switch.

general guidelines are that you can have multiple case statements together, but a case CANNOT fall through (like it can in C/C++) and it has to end in a break;

Muad'Dib
  • 28,542
  • 5
  • 55
  • 68
1

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);
Keith Nicholas
  • 43,549
  • 15
  • 93
  • 156
  • The downvote is probably because this code, even if its correct, and maybe looks cool, its slow compared with a simple switch/case. You create new arrays each time you call it, and then initialize it with variables, do you ever see how this is dissembled ? - and how the contains make the the final search.... ? too slow. – Aristos Sep 03 '12 at 19:26
  • @Aristos, always optimize last.... in this case, because the amount of cases is so small, any savings are in the "nano / microseconds" not exactly highly critical, if it got any longer, I'd maybe go to a hashlist (which is what a switch will turn into if it gets too long also). – Keith Nicholas Sep 03 '12 at 20:59
  • well, if you do not understand how slow is that code... I can not help more. – Aristos Sep 03 '12 at 21:31
0

Each case may represent only a single value, so what you want to do is to make cases without breaks:

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);
Jordaan Mylonas
  • 1,261
  • 1
  • 11
  • 24
0

I believe you are having trouble compiling this, because statements like "Case1,2,7,5" are invalid.

Instead, you need to layer them and use fallthrough/break as appropriate.

I believe this is what you are trying to achieve:

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

}

See relevant question Multiple Cases in Switch

Community
  • 1
  • 1
sparksalot
  • 518
  • 4
  • 7
0

You should write like this:

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;
            }
        }
Xiaodan Mao
  • 1,648
  • 2
  • 17
  • 30