Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 错误CS0150:应为常量//开关语句中出现错误_C#_Switch Statement - Fatal编程技术网

C# 错误CS0150:应为常量//开关语句中出现错误

C# 错误CS0150:应为常量//开关语句中出现错误,c#,switch-statement,C#,Switch Statement,我的switch语句有问题。我不知道如何在c中正确使用它。它给了我错误CS0150,我不知道如何处理它。谢谢你的帮助。非常感谢。 代码被删除了。如果你看到的括号比正常少,请不要提醒我 int e = 0; string caseslol; string multi = "multi"; Console.WriteLine("Hi there!"); Console.WriteLine("Do you want to write a number so I can do cool things

我的switch语句有问题。我不知道如何在c中正确使用它。它给了我错误CS0150,我不知道如何处理它。谢谢你的帮助。非常感谢。 代码被删除了。如果你看到的括号比正常少,请不要提醒我

int e = 0;
string caseslol;
string multi = "multi";

Console.WriteLine("Hi there!");
Console.WriteLine("Do you want to write a number so I can do cool things with it ??");
Console.WriteLine("Write yes to say YES and no to say NO");
string str1 = Console.ReadLine();
if (str1.Contains("yes") == true)
{
    Console.WriteLine("Enter it please");
    e = Convert.ToInt32(Console.ReadLine());
    Console.WriteLine("lol, we got it!");
    Console.WriteLine("you wrote this : {0}", e);
    Console.WriteLine("Now that we got it, we can do cool stuff!");
    Console.WriteLine("Do you want to know what is this stuff ???");
    string yesorno = Console.ReadLine();
    if (yesorno.Contains("yes") == true)
    {
        Console.WriteLine("OK");
        Console.WriteLine("We can do calculating stuff with your number");
        Console.WriteLine("Soo much cool, right ???");
        string yesorno2 = Console.ReadLine();
        if (yesorno2.Contains("yes"))
        {
            Console.WriteLine("Choose first another number and write it");
            int b = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("So let's start with what ???");
            switch (caseslol) 
            {
                case multi:
                    Console.WriteLine(e * b);
            }
        }
    }
//这是输出:

error CS0150:A constant value is expected
Compilation failed: 1 error(s), 0 warnings
compiler exit status 1

对于
案例
,不能使用这样的变量,而必须使用常量


因此,您可以使用字符串
“multi”
(这是您分配给变量的值),而不是将变量
multi
放在case语句中。

case
只能与常量一起使用。应该是

private const string multi = "multi";

switch (caseslol)
{
    case multi:
        Console.WriteLine(e * b);

        // you also need this
        break;
}
或者你直接做

switch (caseslol)
{
    case "multi":
        Console.WriteLine(e * b);

        // you also need this
        break;
}
或者你只是使用

if(string.Equals(caseslol, multi))
{
    Console.WriteLine(e * b);
}


一般来说,我看不出在哪里设置了
casesol

现在我明白了,谢谢,但现在它给了我这个错误:错误CS8070:控制不能通过最后一个case标签'case“multi'脱离switch语句:'@HamzaTout您需要添加一个中断;如果要在
开关中使用字符串,它必须是常量字符串,即
常量字符串multi=“multi”
或直接在
开关的case
语句中使用
“multi”