Switch案例错误而不执行程序

时间:2014-04-19 19:21:56

标签: c#

错误是“控制不能从一个案例标签('案例”B“:')落下来” 我正在尝试执行以下操作: 启动应用程序后,将提示用户登录。第一个操作是通过验证输入的用户名的帐户是否对用户进行身份验证。将用户名与帐户匹配后,让帐户对象验证输入的PIN(请参阅帐户类)。 o验证后,向使用其姓名登录的客户显示欢迎消息,并显示菜单提供选项以获得余额,存入帐户,退出帐户,修改客户信息,显示当前交易,然后退出。

 static void Main(string[] args)
        {
            Account myCustAcc = new Account();
            Transaction myCustTrans = new Transaction();
            string input, choice = "";
            string adminName, userName= "";
            int adminPin, userPin;
            //Login 
            Console.WriteLine("\t\t*****************WELCOME TO BANKING APPLICATION*********************\n");   
            choice = Console.Readline();  
            switch (choice)
            {
                case "A":
                    choice = "Admin";
                    Console.Write("\nAdminName :");
                    adminName = Console.ReadLine();
                    Console.Write("AdminName :");
                    Console.Write("AdminPIN :");
                    adminPin = Convert.ToInt32(Console.ReadLine());
                    //IT IS DEFINE USERNAME AND PASSWORD

                    if (adminName.Equals("admn1") && adminPin.Equals("9999"))
                    {
                        Console.ForegroundColor = ConsoleColor.White;
                        Console.WriteLine("Welcome to Bank");
                    } 
                    break;  
                case "B":
                {
                    choice = "User";
                    Console.Write("\nUserName :");
                    userName = Console.ReadLine();
                    Console.Write("UserName :");
                    Console.Write("PIN :");
                    userPin = Convert.ToInt32(Console.ReadLine());
                    //IT IS DEFINE USERNAME AND PASSWORD

                    if (userName.Equals("SMD") && userPin.Equals("1212"))
                    {
                        Console.ForegroundColor = ConsoleColor.White;
                        //Welcome Message with Name 
                        Console.WriteLine("\t\t\t Welcome to Banking Application",userName);
                        Console.WriteLine("\n<<<Please Select Following Menus>>>");
                        do
                        {
                 //With Menu option to get balance, deposit/withdraw from account, modify customer information display current balance and exit
                            Console.WriteLine("\t1> GetBalance");
                            Console.WriteLine("\t2> Deposit");
                            Console.WriteLine("\t3> Withdraw");
                            Console.WriteLine("\t4> Modify");
                            Console.WriteLine("\t5> Display");
                            Console.WriteLine("\t6> Exit");
                            input = Console.ReadLine();
                            switch (input)
                            {
                                case "1":
                                    break;

                                case "2":
                                    break;

                                case "3":
                                    break;

                                case "4":
                                    break;

                                case "5":
                                    break;

                                case "6":

                                default: Console.WriteLine("Exit the Application!!!");
                                    break;
                            }
                        } while (input != "6");
                      }
                    break;                       
                }                   
       }
        //Pause Display
        Console.WriteLine("Press Any key to continue...........");
        Console.ReadLine();  
    }
}

}

这是我的帐户类

class Account
    {
        //Declare Instance Variables
        private string customerFirstName;
        private string customerLastName;
        private string customerAddress;
        private string customerState;
        private int customerZip;
        private double customerBalance;

        //Class Variables 
        private static string customerUserName;
        private static int customerPin;

        //Retrieve Customer First Name
        public string getCustomerFirstName()
        {
            return customerFirstName;
        }
        //Set Customer Name
        public void setCustomerFirstName(String newFirstName)
        {
            customerFirstName = newFirstName;
        }
        //Retrieve Customer Last Name
        public string getCustomerLastName()
        {
            return customerLastName;
        }
        //Set Customer Last Name
        public void setCustomerLastName(String newLastName)
        {
            customerLastName = newLastName;
        }
        //Retrieve Customer Address
        public string getCustomerAddress()
        {
            return customerAddress;
        }
        //Set Customer Address
        public void setCustomerAddress(string newAddress)
        {
            customerAddress = newAddress;
        }
        //Retrieve Customer State
        public string getCustomerState()
        {
            return customerState;
        }

        //Set Customer State
        public void setCustomerZip(string newState)
        {
            customerState = newState;
        }

        //Retrieve Customer Zip
        public int getCustomerZip()
        {
            return customerZip;
        }

        //Set Customer Zip
        public void setCustomerZip(int newZip)
        {
            customerZip = newZip;
        }

    }

3 个答案:

答案 0 :(得分:2)

您需要将break移到if语句之外。另外,我建议你使用括号作为case语句,因为它们会使意图清晰(语句块的开始和结束)。

您的代码在单个方法中包含太多执行路径,这使得代码难以阅读且难以维护。将代码重构为只做一件事的方法,以便易于维护。

答案 1 :(得分:0)

您的主切换语句正在使用您设置为空字符串然后从不分配的选择变量。看起来你打算在初始消息之后包含一个Console.Read但是忘了它。

答案 2 :(得分:0)

窗口闪烁,因为'choice'变量未初始化为“A”或“B”:

string input, choice = "";  //<----- here

因此切换块无法匹配,程序退出。 发出无法访问的代码警告,因为您在break命令之后放了一些行。

switch (choice)
{
    case "A":
        ...
        break;
    case "B":
        {
            ...
            break; //<--- quits case "B", but the code below belongs to case "B"
        }  

        //Pause Display
        Console.WriteLine("Press the Enter key to Exit");   // <--- here is still considered part of case "B"
        Console.ReadLine();
// case "B" ends here
}   // end of outer switch block.

你可能想要考虑这样的事情:

...
Console.WriteLine("\t\t*****************WELCOME TO BANKING APPLICATION*********************\n");
Console.WriteLine("Choose A or B");
choice = Console.ReadLine();
switch (choice)
{
    case "A":
        ...
        break;
    case "B":
        ...
        break;
}   // end of outer switch block.

//Pause Display
Console.WriteLine("Press the Enter key to Exit");
Console.ReadLine();