c# - 如何将 switch case 操作放入 C# 的循环中?

标签 c# loops switch-statement

我在 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
                    }       
                    }
              }
           }
    }
}

最佳答案

所以这里的主要问题是您需要一个 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:");

所以现在它可以被所有三个 block 重用,abch。这是一个完整的示例,其中包括对防止键入输入的方法的调用。

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;
    }
}

关于c# - 如何将 switch case 操作放入 C# 的循环中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11899620/

相关文章:

php - 如何使用 switch 为一个案例添加两个值?

javascript - case user.length < 4;控制台日志 ("nametoo short :(");休息;为什么这不起作用?

c# - ASP.Net webform 生成 Excel 文件到本地 PC 而不是服务器

c++ - recv() 正确使用 C++

r - 如何让 vapply 返回一个日期向量?

php - 伪先验算法

php - 开关盒 <音频>

c# - 使每个 block 内的更改持久化

c# - 无论如何要跟踪 C# 项目中使用或引用的所有类和接口(interface)?

c# - 如何使用特定参数计算零