控制不能从一个案例标签('案例" 1":')转移到另一个案例标签

时间:2014-04-09 12:17:38

标签: c# switch-statement

我试图在简单的C#控制台程序上显示总价。但是Control不能从一个案例标签('case“1”:')到另一个错误发生。我的错是什么?

    using System;
    class a
    {
        static void Main()
        {

        start: Console.WriteLine("1. Cofee 2. Jam. 3. Bread 4. Apple");
            String a = Console.ReadLine();

            int price = 0;
            switch (a)
            {
                case "1": price += 2;
                case "2": price += 3;
                case "3": price += 4;
                case "4": price += 5;
                    Console.WriteLine("your selected item is {0} Your price is ${1} Do You Want to Continue? YES or NO", a, price);
                    String max = Console.ReadLine();
                    max = max.ToUpper();

                    if (max == "YES")
                    {

                        goto start;

                    }
                    else
                    {

                        break;
                    }


                default: goto start;

            }

        }



    }

6 个答案:

答案 0 :(得分:4)

我理解你的程序是一个循环,你在每次迭代中选择一个项目。该项目的价格应加到总数中。但问题是每次都要加价。

您需要使用break;来停止执行switch

switch (a)
{
    case "1": price += 2; break;
    case "2": price += 3; break;
    case "3": price += 4; break;
    case "4": price += 5; break;
}

switch移除剩余的代码。

您可以在MSDN上找到更多信息:

  

在所选开关部分中执行语句列表从第一个语句开始,然后继续执行语句列表,通常直到跳转语句,例如 break ,goto case,return或throw , 到达了。此时,控制权转移到switch语句之外或转移到另一个案例标签。

答案 1 :(得分:4)

您需要在每个break;之间插入case语句。

此外,如果所选择的选项为1 - 4,您似乎希望该代码显示您的消息。您可以稍微修改您的逻辑以实现此目的,从而无需goto

while (true)
{
    Console.WriteLine("1. Cofee 2. Jam. 3. Bread 4. Apple");
    String a = Console.ReadLine();

    int price = 0;
    switch (a)
    {
        case "1": price += 2; break;
        case "2": price += 3; break;
        case "3": price += 4; break;
        case "4": price += 5; break;
        default: continue;
    }

    Console.WriteLine("your selected item is {0} Your price is ${1} Do You Want to Continue? YES or NO", a, price);
    String max = Console.ReadLine();
    max = max.ToUpper();

    if (max == "NO")
        break;
}

答案 2 :(得分:2)

            case "1": price += 2;
                      goto case "2";
            case "2": price += 3;
                      goto case "3";

答案 3 :(得分:1)

不能通过案例是C#语言规范的限制。你做错了什么是试图堕落。

答案 4 :(得分:0)

为了像行为一样堕落,你需要明确说明你的意思是堕落。这是使用goto完成的。例如:

switch (a)
{
    case "1": price += 2; goto case "2";
    case "2": price += 3; break;
}

http://msdn.microsoft.com/en-us/library/06tc147t.aspx

答案 5 :(得分:0)

你的开关块可能看起来像这样(注意break;语句):

switch (a)
{
    case "1":
        price += 2;
        break;
    case "2":
        price += 3;
        break;
    case "3":
        price += 4;
        break;
    case "4":
        price += 5;
        break;
    default: goto start;
}

看起来您希望此部分代码在切换后运行,而不仅仅是他们选择了Apple。

Console.WriteLine("your selected item is {0} Your price is ${1} Do You Want to Continue? YES or NO", a, price);
String max = Console.ReadLine();
max = max.ToUpper();

if (max == "YES")
{
    goto start;
}
else
{
    break;
}

您可能需要重新整理整个计划。我想这就是你想要做的事情:

using System;
class a
{
    static void Main()
    {

    start: Console.WriteLine("1. Cofee 2. Jam. 3. Bread 4. Apple");
        String a = Console.ReadLine();

        int price = 0;
        switch (a)
        {
            case "1":
                price += 2;
                break;
            case "2":
                price += 3;
                break;
            case "3":
                price += 4;
                break;
            case "4":
                price += 5;
                break;
            default: goto start;
        }
        Console.WriteLine("your selected item is {0} Your price is ${1} Do You Want to Continue? YES or NO", a, price);
        String max = Console.ReadLine();
        max = max.ToUpper();

        if (max == "YES")
        {
            goto start;
        }
    }
}