如题,我去掉其中一个break编译就不让通过。
但是今天看到以下程序。using System;
using System.Collections.Generic;
using System.Text;namespace Exp3_4
{
    class Program
    {
        static void Main(string[] args)
        {
            string myChoice;
            do
            {
                //屏幕输出的列单
                Console.WriteLine("我的地址簿");
                Console.WriteLine("A-增加地址");
                Console.WriteLine("D-删除地址");
                Console.WriteLine("M-修改地址");
                Console.WriteLine("V-查询地址");
                Console.WriteLine("Q-退出\n");
                Console.WriteLine("请选择(A, D, M, V, orQ ):");
                
                //由用户输入选择
                myChoice = Console.ReadLine();
                //根据用户的输入做处理
                switch (myChoice)
                {
                    case "A":            
                    case "a":
                        Console.WriteLine("你的选择是增加地址。");
                        break;                    case "D":
                    case "d":
                        Console.WriteLine("你的选择是删除地址。");
                        break;                    case "M":
                    case "m":
                        Console.WriteLine("你的选择是修改地址。");
                        break;                    case "V":
                    case "v":
                        Console.WriteLine("你的选择是查询地址。");
                        break;                    case "Q":
                    case "q":
                        Console.WriteLine("Bye.");
                        break;
                    //以上情况的都不是的情况下,选择默认方式
                    default:
                        Console.WriteLine("选择{0}不存在", myChoice);
                        break;
                }
                //输出结果
                Console.Write("请继续作选择…按Enter键继续…");
                Console.ReadLine();
                Console.WriteLine();
            }
            //继续要求用户输入,直到用户想退出为止
            while (myChoice != "Q" && myChoice != "q");
        }
    }
}可以编译。奇怪了,是不是因为case语句里面没内容。
求教

解决方案 »

  1.   

    的确每一个case 都需要break
    case "V":
                        case "v":
                            Console.WriteLine("你的选择是查询地址。");
                            break;
    这个意思是说 等于  "V"或者"v"的时候都会执行 Console.WriteLine("你的选择是查询地址。");
    然后跳出。
    这里最好 myChoice ToUpper();全部变为大写 然后再比较。
      

  2.   

    没有内容什么也不做,规范点写上更好,不一定非得写break,用return直接退出方法体也可以的
    上面的程序完全可以把myChoice转换为char然后
    if(myChoice<='z' || myChoice >= 'a')
    {  
         switch....
    }
    这样就可以把大写的分支全部过滤掉,当然具体需求具体分析