1

我在 C# 中做了一个 switch case 语句,它为用户提供了几个可供选择的选项。如果用户输入无效选项,我希望它再次运行(可能通过某种循环)。请帮助我,我相信这是非常基本的。

     static void Main(string[] args)
        {
            int a,b,ch;

            Console.WriteLine("Enter the value of a:");
            a = Convert.ToInt32(Console.ReadLine());   

            Console.WriteLine("Enter the value of b:");
            b = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Enter your choice : Addition:0  Subtraction:1  Multiplication :2 :");
            ch = Convert.ToInt32(Console.ReadLine());
            switch(ch)
            {
                case 0: {
                    Console.WriteLine("Addition value is :{0}", a + b);
                    break;
                }
                case 1:
                    {
                        Console.WriteLine("Subtraction value is :{0}", a - b);
                        break;
                    }
                case 2:
                    {
                        Console.WriteLine("Multiplication value is :{0}", a * b);
                        break;
                    }
                default:
                    {
                        Console.WriteLine("Invalid choice ");
                        goto switch(ch); 

//please tell me what should i write here, it should go to the start of the switch case
                    }
                case 4:
                    {
                         continue; 

 //please tell me what should i write here.it should come out of the loop show the result
                    }       
                    }
              }
           }
    }
}
4

8 回答 8

4

所以这里的主要问题是你需要一个while循环来停留,并且可以选择中断。不过,这里还有一些其他有趣的事情是,您确实需要更好地验证Type用户的输入。例如,这两行:

Console.WriteLine("Enter the value of a:"); 
a = Convert.ToInt32(Console.ReadLine());    

真的应该替换为:

while (true)
{    
    Console.WriteLine("Enter the value of a:"); 
    if (Int32.TryParse(Console.ReadLine(), out a))
    {
        break;
    }
}

同样,您还有其他三个地方正在做同样的事情,所以我建议构建一个方法并调用它——它可能看起来像这样。

private static int GetIntegerInput(string prompt)
{
    int result;
    Console.WriteLine();

    while (true)
    {
        // THIS SHOULD OVERWRITE THE SAME PROMPT EVERY TIME
        Console.Write(prompt); 
        if (Int32.TryParse(Console.ReadLine(), out result))
        {
            break;
        }
    }
    return result;
}

然后你会这样称呼它:

a = GetIntegerInput("Enter the value of a:");

所以现在可以对所有三个块重复使用a,bch. 这是一个完整的示例,其中包括对防止键入输入的方法的调用。

static void Main(string[] args) 
{ 
    int a,b,ch; 

    while (ch != 4)
    { 
        // GET READY TO ASK THE USER AGAIN
        Console.Clear();

        a = GetIntegerInput("Enter the value of a:");
        b = GetIntegerInput("Enter the value of b:");
        ch = GetIntegerInput("Enter your choice : Addition:0  Subtraction:1  Multiplication :2 :");

        switch(ch) 
        { 
            case 0:
            { 
                Console.WriteLine("Addition value is :{0}", a + b); 
                break; 
            } 
            case 1: 
            { 
                Console.WriteLine("Subtraction value is :{0}", a - b); 
                break; 
            } 
            case 2: 
            { 
                Console.WriteLine("Multiplication value is :{0}", a * b); 
                break; 
            } 
            default: 
            { 
                Console.WriteLine("Invalid choice "); 

                // THIS GOES TO THE BEGINNING OF THE LOOP
                // SO THAT YOU CAN ASK THE USER AGAIN FOR
                // MORE CORRECT INPUT
                continue;
            }
        }

        // THIS WILL BREAK YOU OUT OF THE LOOP ON A GOOD ENTRY
        break;
    }
}
于 2012-08-10T10:21:38.877 回答
2

您没有循环 int ch 将默认为 0。因此您只会进入 case 0。您需要包含goto case 1;

查看http://www.dotnetperls.com/switch了解更多信息

于 2012-08-10T10:21:59.840 回答
1

您的代码很容易修复。你没有循环开始!

int a = 0, b = 0, ch = -1; //always initialize your variables.

do 
    Console.WriteLine("Enter the value of a:");
while(!int.TryParse(Console.ReadLine(), out a));

do 
    Console.WriteLine("Enter the value of b:");
while(!int.TryParse(Console.ReadLine(), out b));

while (ch != 4) //starts at -1 so it will surely enter the loop
{
    //Will keep asking until user enters "4", then it will exit
    do
        Console.WriteLine("Enter your choice : Addition:0  Subtraction:1  Multiplication :2 :");
    while(!int.TryParse(Console.ReadLine(), out ch));

    switch (ch)
    {
        case 0:
            {
                Console.WriteLine("Addition value is :{0}", a + b);

            } break;
        case 1:
            {
                Console.WriteLine("Subtraction value is :{0}", a - b);

            } break;
        case 2:
            {
                Console.WriteLine("Multiplication value is :{0}", a * b);

            } break;
        // case 4 is not needed, it will exit from the loop anyway
        default:
            {
                Console.WriteLine("Invalid choice");
            } break;
    }
}

编辑:我添加了一个粗略的错误检查,如果用户输入“A”而不是数字,则不会让代码崩溃。

于 2012-08-10T10:26:55.613 回答
1

与其让用户输入不正确的内容然后再次运行直到他们正确,为什么不给他们一组严格的选择呢?请参见下面的代码:

using System;

public static class Program
{
    static void Main()
    {
        const int addition = 0;
        const int subtraction = 1;
        const int multiplication = 2;

        var a = GetInt32("Enter the value of a:");  
        var b = GetInt32("Enter the value of b:");

choose:        
        var choice = GetInt32(string.Format(@"Enter your choice:
            {0}: Addition
            {1}: Subtraction
            {2}: Multiplication", addition, subtraction, multiplication));  

        switch(choice)
        {
            case addition:
                {
                    Console.WriteLine("Addition value is :{0}", a + b);
                    break;
                }
            case subtraction:
                {
                    Console.WriteLine("Subtraction value is :{0}", a - b);
                    break;
                }
            case multiplication:
                {
                    Console.WriteLine("Multiplication value is :{0}", a * b);
                    break;
                }
            default:
                {
                    Console.WriteLine("Invalid choice ");
                    goto choose; 
                }
        }
    }

    private static int GetInt32(string prompt)
    {
        while(true)
        {
            Console.WriteLine(prompt);
            var line = Console.ReadLine();
            int result;
            if(int.TryParse(line, out result))
                return result;
        }
    }
}
于 2012-08-10T10:29:18.673 回答
0
    static void Main(string[] args)
    {
        int a, b, ch;

        Console.WriteLine("Enter the value of a:");
        a = Convert.ToInt32(Console.ReadLine());

        Console.WriteLine("Enter the value of b:");
        b = Convert.ToInt32(Console.ReadLine());
        start:
        Console.WriteLine("Enter your choice : Addition:0  Subtraction:1  Multiplication :2 :");

        ch = Convert.ToInt32(Console.ReadLine());

        switch (ch)
        {
            case 0: Console.WriteLine("Addition value is :{0}", a + b);
                break;

            case 1: Console.WriteLine("Subtraction value is :{0}", a - b);
                break;

            case 2: Console.WriteLine("Multiplication value is :{0}", a * b);
                break;

            default: Console.WriteLine("Invalid choice ");
                ch = 0;
                goto start;
        }
    }
于 2012-08-10T10:41:18.523 回答
0
int i = 0;
while(i == 0)
{
    i == 1; // this will make it exit the loop unless case 4 happens   
    switch(ch)
    {
        case 0: {
            Console.WriteLine("Addition value is :{0}", a + b);
            break;
        }
        ...
        case 4 {
            i == 0;
            break;
        }
     }
}
于 2012-08-10T10:21:04.533 回答
0
int ch = -1;
while(ch!= 0 && ch!= 1 && ch!=2)
    ch = Convert.ToInt32(Console.ReadLine());
switch(ch)   //correct input
于 2012-08-10T10:21:17.017 回答
0
    static void Main(string[] args)
    {
        int firstValue, secondValue, arithmeticOperation;
    RestartProgram:
        Console.WriteLine("Enter the value of a:");
        firstValue = Convert.ToInt32(Console.ReadLine());

        Console.WriteLine("Enter the value of b:");
        secondValue = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("Enter your choice : Addition:0  Subtraction:1  Multiplication :2 :");

        arithmeticOperation = Convert.ToInt32(Console.ReadLine());
        switch (arithmeticOperation)
        {
            case 0:
                {
                    Console.WriteLine("Addition value is :{0}", firstValue + secondValue);
                    break;
                }
            case 1:
                {
                    Console.WriteLine("Subtraction value is :{0}", firstValue - secondValue);
                    break;
                }
            case 2:
                {
                    Console.WriteLine("Multiplication value is :{0}", firstValue * secondValue);
                    break;
                }
            default:
                {
                    Console.WriteLine("Invalid choice ");
                    goto RestartProgram;
                }
        }

        Console.ReadLine();
    }

这是你正在寻找的吗?

于 2012-08-10T10:25:58.157 回答