当用户使用switch语句选择选项时,如何不让程序结束并重复自身

时间:2015-08-13 19:52:26

标签: c# loops

请注意我对编程和C#有相当新的意义。请以我理解的方式表达您的答案。

我正在尝试使用命令窗口在C#中创建一个小文本冒险。我有一个问题,我希望每次选择其中一个选项时给用户两个选项/案例。我相信我需要使用循环来实现这一点,但是,我不确定如何将它实现到我的代码中。

        Console.WriteLine("Welcome to my Battle Runners game!");
        Console.WriteLine("");
        Console.WriteLine("Chose an option from the list.");
        Console.WriteLine("1. Start Game");
        Console.WriteLine("2. About");
        Console.WriteLine("3. Credits and Information");

        String stringMenuOption = Console.ReadLine();
        int menuOption = Convert.ToInt32(stringMenuOption);

        switch (menuOption)
        {
            case 1:
                Console.WriteLine("");
                Console.WriteLine("Loading the game...");
                //all game code goes here
                break;
            case 2:
                Console.WriteLine("");
                Console.WriteLine("Battle Runners, developed by " + author +                 " is a text adventure that follows the life of a soldier in war.");
                Console.WriteLine("");
                break;
            case 3:
                Console.WriteLine("");
                Console.WriteLine("Author(s): " + author);
                Console.WriteLine("Version: " + version);
                Console.WriteLine("Date created: " + created);
                break;
            default:
                Console.WriteLine("");
                Console.WriteLine("You typed something incorrect!");
                break;
        }







    }
}

}

我不希望程序在用户选择其中一个案例时结束,或者"选项"。每次案例二和三运行在用户的请求,我想让所有的情况(或#34;选项")再次重复给用户,而不是刚刚结束的程序。 当第一个案例运行时,我不希望这种情况发生,但继续使用我的其余代码。

1 个答案:

答案 0 :(得分:4)

只需在循环中包装所有内容并在选择quit选项时退出循环。

int menuOption;
do
{
    Console.WriteLine("Welcome to my Battle Runners game!");
    Console.WriteLine("");
    Console.WriteLine("Chose an option from the list.");
    Console.WriteLine("1. Start Game");
    Console.WriteLine("2. About");
    Console.WriteLine("3. Credits and Information");
    Console.WriteLine("4. Quit");

    String stringMenuOption = Console.ReadLine();
    menuOption = Convert.ToInt32(stringMenuOption);

    switch (menuOption)
    {
        case 1:
            Console.WriteLine("");
            Console.WriteLine("Loading the game...");
            //all game code goes here
            break;
        case 2:
            Console.WriteLine("");
            Console.WriteLine("Battle Runners, developed by " + author +                 " is a text adventure that follows the life of a soldier in war.");
            Console.WriteLine("");
            break;
        case 3:
            Console.WriteLine("");
            Console.WriteLine("Author(s): " + author);
            Console.WriteLine("Version: " + version);
            Console.WriteLine("Date created: " + created);
            break;
        case 4:
            break;
        default:
            Console.WriteLine("");
            Console.WriteLine("You typed something incorrect!");
            break;
    }
} while (menuOption != 4);

注意:代码未经测试/未编译。