如何在case语句中调用函数?

时间:2016-07-18 00:51:05

标签: c# visual-studio

您好,我是新手,我想在其他案例陈述中调用功能菜单,这样当我按1时,它会直接转到标有杂货的案例。我已经彻底解释了自己。

static void menu()
{
    Console.WriteLine("Select a category to view");
    Console.WriteLine("");
    Console.WriteLine("1.Groceries");
    Console.WriteLine("2.Electronics & Appliances");
    Console.WriteLine("3.Exit");
    Console.ReadKey();

    int response = int.Parse(Console.ReadLine());
    switch (response)
    {
        case 1:
            Console.WriteLine("...........Groceries...............");
            break;

        case 2:
            Console.WriteLine("..............Electronics & Appliances............");
            break;
        case 3:
            Console.WriteLine("...........Exit...............");
            break;
    }
}

static void Main(string[] args)
{
    menu();

    Console.WriteLine(@" Choose items being purchased from Groceries /n      
          1:stove = 3000 \n 2: potcollection = 2000 \n
          3:lemonsqeezer = 1000 \n 4:oven = 10000 \n 5:blender = 6000");
    double stove = 3000;
    double lemonsqeezer = 1000;
    double oven = 10000;
    double blender = 6000;
    double potcollection = 2000;

    Console.WriteLine("Enter a number from the above groceries list");

    int response = int.Parse(Console.ReadLine());
    if (response == 1)
        switch (response)
        {
            case 1:
                Console.WriteLine("The total is{0}",);
                break;
            case 2:
                Console.WriteLine("The total is {0}",);
                break;
        }
}

3 个答案:

答案 0 :(得分:2)

您始终处理选项1,因为您的if包含switch声明。

if (response == 1) // no other response other than 1 will get processed.
    switch (response)
    {
        case 1:
            Console.WriteLine("The total is{0}",);
            break;
        case 2:
            Console.WriteLine("The total is {0}",);
            break;
    }
}

删除if,其他case语句将被执行。

答案 1 :(得分:1)

你已经在做了。

switch (response)
{
    case 1:
        Console.WriteLine("...........Groceries...............");

您从Console.WriteLine致电case。你可以用同样的方式调用另一种方法或功能。

答案 2 :(得分:0)

我修改了你的代码:

static void menu()
{
    double[] groceries = new double[] { 3000, 1000, 10000, 6000, 2000 }; // By creating an array, you can easily get the price of an item by the index.
    // double[] groceries = new double[] { stove, lemonsqeezer, oven, blender, potcollection };

    // double[] electronics_and_appliances = new double[] { ... };

    Console.WriteLine("Select a category to view");
    Console.WriteLine("");
    Console.WriteLine("1.Groceries");
    Console.WriteLine("2.Electronics & Appliances");
    Console.WriteLine("3.Exit");

    int response = int.Parse(Console.ReadLine());

    switch (response)
    {
        case 1:
            Console.WriteLine("...........Groceries...............");
            Console.WriteLine("");
            Console.WriteLine(@" Choose items being purchased from Groceries /n      
               1:stove = 3000 \n 2: potcollection = 2000 \n
               3:lemonsqeezer = 1000 \n 4:oven = 10000 \n 5:blender = 6000");
            Console.WriteLine("");
            Console.WriteLine("Enter a number from the above groceries list");

            // This prevents IndexOutOfRangeException.
            try
            {
                Console.WriteLine("The total is {0}", groceries[int.Parse(Console.ReadLine()) - 1]); // Minus one becuase index's start from 0.
            }
            catch
            {
                Console.WriteLine("That item doesn't exist");
            }

            break;

        case 2:
            Console.WriteLine("..............Electronics & Appliances............");
            /*

            Console.WriteLine("");
            Console.WriteLine(@"Choose items being purchased from Electronics & Appliances/n ...");
            Console.WriteLine("");
            Console.WriteLine("Enter a number from the above Electronics & Appliances list");

            try
            {
                Console.WriteLine("The total is {0}", electronics_and_appliances[int.Parse(Console.ReadLine()) - 1]);
            }
            catch
            {
                Console.WriteLine("That item doesn't exist");
            }

            */
            break;
        case 3:
            Console.WriteLine("...........Exit...............");
            break;
    }
}

static void Main(string[] args)
{
    menu();
}