0

我一直在尝试将 If Else 语句转换为 switch,但它给出了一个错误,它需要一个常量:

这是我一直在处理的代码:

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();

        }

    }
}

这是我要转换的内容:

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();

        }

    }
}
4

1 回答 1

5

我认为您需要先了解开关。开关应用于变量,在本例中为“ME”。此外,switch-case 不能有“范围”条目,只有“精确”。所以你的代码看起来像:

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();

你也犯了一个复制粘贴错误:if (0 < ME && ME < 12)应该if (0 > ME && ME < 12)

要寻求仅开关解决方案,您可以使用跌落槽案例。但我认为这很丑陋,使代码更难阅读。

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();
于 2012-07-05T09:30:14.703 回答