循环遍历C#中的菜单项

时间:2015-12-02 05:20:04

标签: loops

我正在尝试制作一个菜单,我可以添加一些视频游戏信息,直到我决定退出该程序。到目前为止,我有这个:

    do
    {
        Console.WriteLine("Select the number of your choice: ");
        Console.WriteLine("1: Add a game");
        Console.WriteLine("2: Remove a game");
        Console.WriteLine("3: List games");
        Console.WriteLine("4: Save games");
        Console.WriteLine("5: Read games from file");
        Console.WriteLine("6: Quit");
        Console.Write("Enter the number of your choice: ");
        userChoice = Console.ReadLine();
    } while (!Int32.TryParse(userChoice, out num)); //checks to see if the input is a number
    if (userChoice == "6")
    {
        Environment.Exit(0);
    }

代码在显示选项后停止。我在显示选项后尝试再次显示菜单,以便用户可以选择另一个号码。最终,我希望它看起来像这样:

直到用户输入6并退出程序。为了达到这个目的,我可以做些什么编辑?

5 个答案:

答案 0 :(得分:3)

要继续循环,你的所有逻辑都必须在里面循环!

    int num;
    string userChoice;

    do
    {
        Console.WriteLine("Select the number of your choice: ");
        Console.WriteLine("1: Add a game");
        Console.WriteLine("2: Remove a game");
        Console.WriteLine("3: List games");
        Console.WriteLine("4: Save games");
        Console.WriteLine("5: Read games from file");
        Console.WriteLine("6: Quit");
        Console.Write("Enter the number of your choice: ");
        userChoice = Console.ReadLine();

        if(!Int32.TryParse(userChoice, out num)) continue;

        if (userChoice == "6")
        {
            Environment.Exit(0);
        }

        Console.WriteLine("Choice = " + userChoice);

        if(userChoice == "1")
        {
            //Some code that will allow me to enter info on some game
        // it will then display the menu again after all questions are answered.
        }

    } while (true);

答案 1 :(得分:2)

将if检查放在do while循环中。您正在循环外检查它们,因此没有任何处理。将它们放在do while循环中或将它们放在一个函数中,并在从用户那里获取输入后调用该函数。

答案 2 :(得分:1)

你需要从while条件中删除!,如果用户输入6看下面的代码片段,还需要一些打破该循环的方法:

    int num = 0;
    string userChoice = string.Empty;
    do
    {
        Console.WriteLine("Select the number of your choice: ");
        Console.WriteLine("1: Add a game");
        Console.WriteLine("2: Remove a game");
        Console.WriteLine("3: List games");
        Console.WriteLine("4: Save games");
        Console.WriteLine("5: Read games from file");
        Console.WriteLine("6: Quit");
        Console.Write("Enter the number of your choice: ");
        if(num == 6)
            break;
        else
            userChoice = Console.ReadLine();

    }
    while (Int32.TryParse(userChoice, out num)); //checks to see if the input is a number
    if (userChoice == "6")
    {
        Environment.Exit(0);
    }

完整代码为here

答案 3 :(得分:0)

int num;
 string userChoice;
 do
 {
     Console.WriteLine("Select the number of your choice: ");
     Console.WriteLine("1: Add a game");
     Console.WriteLine("2: Remove a game");
     Console.WriteLine("3: List games");
     Console.WriteLine("4: Save games");
     Console.WriteLine("5: Read games from file");
     Console.WriteLine("6: Quit");
     Console.Write("Enter the number of your choice: ");
     userChoice = Console.ReadLine();
 } while (Int32.Parse(userChoice) != 6);

答案 4 :(得分:0)

删除'!'来自while (!Int32.TryParse(userChoice, out num)); Read Documentation