2

这是一个可用值菜单的示例。如果没有选择任何选项,我想让它循环回到开始。

static void Main(string[] args)
{
    Console.WriteLine("1 : Option 1");
    Console.WriteLine("2 : Option 2");
    Console.WriteLine("3 : Option 3");
    Console.WriteLine("4 : Option 4");
    Console.WriteLine("5 : Option 5");
    Console.Write("Please enter your option choice: ");
    string choice = Console.ReadLine();

    int intChoice = int.Parse(choice);
    switch (intChoice)
    {
        case 1:
            Console.WriteLine("you chose 1");
            break;
        case 2:
            Console.WriteLine("you chose 2");
            break;
        case 3:
            Console.WriteLine("you chose 3");
            break;
        case 4:
            Console.WriteLine("you chose 4");
            break;
        case 5:
            Console.WriteLine("you chose 5");
            break;
    }
}

我试图通过使用类和方法来做到这一点,但我真的很困惑。

预先感谢您的任何帮助。

4

6 回答 6

2

将整个东西包装在一个do-while块中:

bool isValid = true;

do
{
    isValid = true;
    // Write to console
    // read from console
    switch(intChoice)    
    {
        // Place some cases here.
        default: 
            Console.WriteLine("Invalid Choice")
            isValid = false;
    }
}
while(!isValid);
于 2012-10-23T21:44:06.030 回答
0

使用布尔值作为翻转开关,告诉while循环是否继续运行。这是一个小例子:

bool stillRunning = true;

while (stillRunning)
{
    Console.WriteLine("Enter a number.");
    string input = Console.ReadLine();
    int key = Convert.ToInt32(input);

    switch (key)
    {
        case 1:
            // Do something.
            stillRunning = false;
            break;

        case 2:
            // Do something.
            stillRunning = false;
            break;

        default:
            Console.WriteLine("No key selected.");
            break;
    }
} 
于 2012-10-23T21:36:51.973 回答
0

你可以使用默认

    switch (intChoice)
    {
        case 1:
            Console.WriteLine("you chose 1");
            break;
        case 2:
            Console.WriteLine("you chose 2");
            break;
        ....... 
        default: 
            //your logic here
            break;
    }

之后,您可以选择如何做。你可以像这样使用一段时间和一个布尔值:

static void Main(string[] args)
        {

            Console.WriteLine("1 : Option 1");
            Console.WriteLine("2 : Option 2");
            Console.WriteLine("3 : Option 3");
            Console.WriteLine("4 : Option 4");
            Console.WriteLine("5 : Option 5");
            Console.Write("Please enter your option choice: ");


            bool correct = true;

            while (correct)
            {
                string choice = Console.ReadLine();
                int intChoice = int.Parse(choice);
                switch (intChoice)
                {
                    case 1:
                        Console.WriteLine("you chose 1");
                        break;
                    case 2:
                        Console.WriteLine("you chose 2");
                        break;
                    case 3:
                        Console.WriteLine("you chose 3");
                        break;
                    case 4:
                        Console.WriteLine("you chose 4");
                        break;
                    case 5:
                        Console.WriteLine("you chose 5");
                        break;
                    default:
                        correct = false;
                        break;
                }
            }
        }
于 2012-10-23T21:41:50.597 回答
0

最明显的解决方案似乎是:

bool loop = true;

while (loop)
{
    loop = false;
    switch (Console.ReadLine())
    {
        case "1":
            Console.WriteLine("you chose 1");
            break;
        case "2":
            Console.WriteLine("you chose 2");
            break;
        case "3":
            Console.WriteLine("you chose 3");
            break;
        case "4":
            Console.WriteLine("you chose 4");
            break;
        case "5":
            Console.WriteLine("you chose 5");
            break;
        default:
            loop = true;
            break;
    }
}

不过,可能有更好的方法来做到这一点。

于 2012-10-23T21:42:15.123 回答
0

因此您只需要一个数字,最好使用GetKey方法,然后从控制台读取string值,然后将其解析为int

DisplayOptions();
bool choiceDone;

do
{
    choiceDone = true;

    switch(GetChoice())
    {
       case ConsoleKey.D1:
           Console.WriteLine("you chose 1");
           break;
       case ConsoleKey.D2:
           Console.WriteLine("you chose 2");
           break;
           // etc
       default:
           choiceDone = false;
           break;
    }

} while(!choiceDone);    

我还提取了几种方法,使代码更干净:

private ConsoleKey GetChoice()
{
     Console.Write("Please enter your option choice: ");
     return Console.ReadKey().Key;
}

private void DisplayOptions()
{
     Console.Clear();
     Console.WriteLine("1 : Option 1");
     Console.WriteLine("2 : Option 2");
     Console.WriteLine("3 : Option 3");
     Console.WriteLine("4 : Option 4");
     Console.WriteLine("5 : Option 5");
}
于 2012-10-23T21:46:05.750 回答
0
public void GetInput()
{
    int inputValue = 0;
    bool isValidInput = false;
    List<int> validEntries = new List<int> { 1,2,3, 42, 55, 69};

    while (!isValidInput)
        isValidInput = int.TryParse(Console.ReadLine(), out inputValue) && validEntries.Contains(inputValue);

    switch (inputValue)
    {
        case 1:
            {
                // something
                break;
            }
        case 2:
            {
                // something else
                break;
            }
        default:
            {
                //yet something else
                break;
            }
    }
}

编辑:添加了显式值检查而不是接受任何整数。

于 2012-10-23T21:49:58.567 回答