如何将我的switch case操作放在C#的循环中?

时间:2012-08-10 10:16:16

标签: 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
                    }       
                    }
              }
           }
    }
}

8 个答案:

答案 0 :(得分: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:");

现在可以重复使用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;
    }
}

答案 1 :(得分:2)

你没有循环int ch将默认为0.因此你只需要进入案例0.你需要包括goto case 1;

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

答案 2 :(得分: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”而不是数字,则不要让代码爆炸。

答案 3 :(得分: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;
        }
    }
}

答案 4 :(得分: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;
        }
     }
}

答案 5 :(得分:0)

int ch = -1;
while(ch!= 0 && ch!= 1 && ch!=2)
    ch = Convert.ToInt32(Console.ReadLine());
switch(ch)   //correct input

答案 6 :(得分: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();
    }

这是你找到的吗?

答案 7 :(得分: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;
        }
    }