0

I've been trying to convert an If Else statement to switch but it gives an error that it needs a constant:

Here is the code I've been working on:

using System;

namespace ConsoleApplication6

{
    class Program
    {
        static void Main()
        {
            String me;
            int ME = 0, YOU = 5, THEY = 20, THEM = 30;
            Console.WriteLine("Default Values: YOU = 5, THEY = 20 and THEM = 30\nValue for \"ME\" >>");
            me = Console.ReadLine();
            ME = Int16.Parse(me);

            switch(true) {
                case (ME > 0 && ME < 12) :  
                 YOU = ME;
                 Console.WriteLine("Value of YOU is {0}", YOU);
                 break;

                case ( ME == 15):
                YOU = THEY * THEM;
                Console.WriteLine("Value of YOU is {0}", YOU);
                break;

                case (ME == 20):
                YOU++;
                Console.WriteLine("Value of YOU is {0}", YOU);
                break;

                case (ME == 17):
                YOU = YOU - 4;
                Console.WriteLine("Value of YOU is {0}", YOU);
                break;

                default:
                Console.WriteLine("You've entered {0}", ME);
                Console.WriteLine("Value of YOU is {0}", YOU);
                break;


           }
            Console.WriteLine("Press Enter to EXIT");
            Console.ReadLine();

        }

    }
}

and here is what I'm trying to convert:

using System;

namespace ConsoleApplication5
{
    class Program
    {
        static void Main()
        {
            String me;
            int ME = 0, YOU = 5, THEY = 20, THEM = 30;
            Console.WriteLine("Default Values: YOU = 5, THEY = 20 and THEM = 30\nValue for \"ME\" >>");
            me = Console.ReadLine();
            ME = Int16.Parse(me);

            if (0 < ME && ME < 12)
            {
                YOU = ME;
                Console.WriteLine("Value of YOU is {0}", YOU);
            }
            else if( ME == 15)
            {
                YOU = THEY * THEM;
                Console.WriteLine("Value of YOU is {0}", YOU);
            }

            else if (ME == 20)
            {
                YOU++;
                Console.WriteLine("Value of YOU is {0}", YOU);
            }

            else if (ME == 17)
            {
                YOU = YOU - 4;
                Console.WriteLine("Value of YOU is {0}", YOU);
            }

            else
            {
                Console.WriteLine("You've entered {0}", ME);
                Console.WriteLine("Value of YOU is {0}", YOU);

            }
            Console.WriteLine("Press Enter to EXIT");
            Console.ReadLine();

        }

    }
}
3
  • 3
    Please don't use (Need help ASAP!) in your question. We can see that you need help, that's obvious because you are posting a question, and as far as the ASAP is concerned, well, that's not our concern. Depending on the quality of your question you will get answers sooner or later. Commented Jul 5, 2012 at 9:31
  • Well there's not much sense in changing it at all is it? Well maybe for readability reasons but everything else is just the same. As PoweRoy mentioned, you need to understand switch-statements first. If you afterwards still think you need to refactor your code, you should have look at "real" refactoring-patterns like replace-conditional-with-polymorphism Commented Jul 5, 2012 at 9:35
  • Well I really havn't found any problem... Its working fine with me.... Can you explain a lillte bit more about your problem. Commented Jul 5, 2012 at 9:46

1 Answer 1

5

I think you will need to understand a switch first. The switch is applied to a variabled, in this case 'ME'. Also a switch-case cannot have a 'range' entry, only 'exact'. So your code will look like:

String me;
int ME = 0, YOU = 5, THEY = 20, THEM = 30;
Console.WriteLine("Default Values: YOU = 5, THEY = 20 and THEM = 30\nValue for \"ME\" >>");
me = Console.ReadLine();
ME = Int16.Parse(me);

if ((0 > ME) && (ME < 12))
{
    YOU = ME;
    Console.WriteLine("Value of YOU is {0}", YOU);
}
else 
{
    switch (ME)
    {
        case 15:
            YOU = THEY * THEM;
            break;
        case 20:
            YOU++;
            break;
        case 17:
            YOU = YOU - 4;
            break;
        default:        
            break;
    }

    Console.WriteLine("You've entered {0}", ME);
    Console.WriteLine("Value of YOU is {0}", YOU);
}
Console.WriteLine("Press Enter to EXIT");
Console.ReadLine();

Also you made a copy paste error: if (0 < ME && ME < 12) should if (0 > ME && ME < 12).

To go for a switch only solution you could use fall-trough cases. But I think this is ugly and makes the code harder to read.

String me;
int ME = 0, YOU = 5, THEY = 20, THEM = 30;
Console.WriteLine("Default Values: YOU = 5, THEY = 20 and THEM = 30\nValue for \"ME\" >>");
me = Console.ReadLine();
ME = Int16.Parse(me);

switch (ME)
{
    case 1:
    case 2:
    case 3:
    case 4:
    case 5:
    case 6:
    case 7:
    case 8:
    case 9:
    case 10:
    case 11:
        YOU = ME;
        break;          
    case 15:
        YOU = THEY * THEM;
        break;
    case 20:
        YOU++;
        break;
    case 17:
        YOU = YOU - 4;
        break;
    default:        
        break;
}

Console.WriteLine("You've entered {0}", ME);
Console.WriteLine("Value of YOU is {0}", YOU);

Console.WriteLine("Press Enter to EXIT");
Console.ReadLine();
Sign up to request clarification or add additional context in comments.

9 Comments

but my professor wanted it all to be in case. (Sorry for being a douche) I'm new here. :)
Your repeating case 20: twice first time to do YOU++ and second time to break;
@Jasper what do you mean he "wanted it all to be in case"? You have to have a variable to switch on. So it has to be in the switch(<here>).
@PoweRoy (0 < ME && ME < 12) means ME>0 and ME<12 I guess that makes sense hmmm :)
Yeah just found out and updated the answer before you posted :)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.