返回菜单选择的功能;项目转换声明

时间:2019-03-13 19:19:31

标签: c# function switch-statement

我必须先创建一个函数以返回所选择的菜单选项,然后返回CLIENT_GET_FILE_NAME语句以显示按下了哪个键,但是在功能部分遇到了问题。

:block2.FILE_NAME := CLIENT_GET_FILE_NAME(File_Filter=> 'CSV Files (*.Csv)|*.Csv|');

}

3 个答案:

答案 0 :(得分:0)

您应该做的是修改getChoice方法以返回一个字符串,该字符串将成为选定的选项。然后在getChoice方法中编写Console.ReadLine并将值放入变量中并返回该变量。这样,您可以在displayMenu 1之后调用getChoice函数并获取值。

我会自己提供代码,但是由于这更多的是一项家庭作业,所以我认为最好让您理解这一点并尝试自己用这个想法来做:)!

希望这会有所帮助!

答案 1 :(得分:0)

请考虑Console.ReadKey()将等待,直到您按下某个键才能继续。完成后,属性Console.ReadKey()。Key将包含按下的键的值 所以,在这种情况下...

var key = Console.ReadKey().Key;
switch (key) {
    case ConsoleKey.D1:
        Console.WriteLine("You pressed 1");
        break;
    case ConsoleKey.D2:
        Console.WriteLine("You pressed 2");
        break;
    //etc...//
    default:
        Console.WriteLine("You pressed something else");
        break;
}

答案 2 :(得分:0)

所以我觉得这是进步,但是我可能错了? Switch语句会替换我的getChoice函数中的If语句吗?

class Program
{   
    static void displayMainMenu()
    {
        string title = "Old Yeller Pet Store";
        Console.WriteLine("\n\n");
        Console.WriteLine(String.Format("{0," + ((Console.WindowWidth /2) + (title.Length / 2 )) + "}", title));
        Console.WriteLine("\n");

        title = "Main Menu";
        Console.WriteLine("\n\n");
        Console.WriteLine(String.Format("{0," + ((Console.WindowWidth /2) + (title.Length / 2 )) + "}", title));
        Console.WriteLine("\n");

        string line = "1.    Buy a Pet";
        Console.WriteLine(line.PadLeft(line.Length+1 + 50));

        string line2 = "2.    Buy Food";
        Console.WriteLine(line2.PadLeft(line2.Length+1 + 50));

        string line3 = "3.    File OPs";
        Console.WriteLine(line3.PadLeft(line3.Length+1 + 50));

        string line4 = "4.    Manager";
        Console.WriteLine(line4.PadLeft(line4.Length+1 + 50));

        string line5 = "5.    Quit";
        Console.WriteLine(line5.PadLeft(line5.Length+1 + 50));


    }

    static void getChoice()
    {
        string option;
        int choice;
        Console.WriteLine("Please input which number option you choose to use");
        option = Console.ReadLine();
        choice = Convert.ToInt32(option);
        if (choice == 1)
        {
            Console.Write("Lipsum");
            //code to open Buy a Pet menu
        }



    }


    public static void Main(string[] args)
    {
        displayMainMenu ();
        getChoice();



        Console.ReadKey();

    }
}