Switch情况下出错,未执行程序
本文关键字:执行程序 出错 情况下 Switch | 更新日期: 2023-09-27 17:59:12
错误为"控件不能从一个大小写标签('case"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;
}
}
您需要将break
移到if
语句之外。此外,我建议您对case语句使用brakes,因为它们会使意图变得清晰(语句块的起点和终点)。
您的代码在单个方法中有太多的执行路径,这使得代码难以读取和维护。将代码重构为只做一件事的方法,这样就很容易维护。
您的主开关语句使用的是选择变量,您将该变量设置为空字符串,然后从不赋值。看起来你打算包括一个控制台。在最初的消息后阅读,但忘记了。
由于'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();