我一直在尝试将 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();
}
}
}