C#控制台应用新注册用户登录功能实现问题求助
C#银行控制台应用登录问题修复方案
我开发C#银行控制台应用时,遇到新注册账号无法登录的问题,目前还在完善注册模块的空值/空格错误提示逻辑,代码暂未添加注释,核心诉求是解决用新注册数据实现登录的问题。
问题代码
主程序代码
class BankAccount { public static void MainMenu() { Console.Clear(); Console.WriteLine("Welcome to Bam's Bank of NSW. \nPlease select from the following menu: \n1: Login \n2: Sign up \n3: Quit"); int input = Convert.ToInt32(Console.ReadLine()); switch (input) { case 1: Login(); break; case 2: SignUp(); break; case 3: Program.Quit(); break; } } public static void WelcomeMenu() { Console.Clear(); Console.WriteLine("Welcome John \nPlease select from the following menu: \n1: View Balance \n2: Deposit \n3: Withdraw \n4: Transfer \n5: Quit"); int input1 = Convert.ToInt32(Console.ReadLine()); switch (input1) { case 1: Bank.ViewBalance(); break; case 2: Bank.Deposit(); break; case 3: Bank.Withdraw(); break; case 4: Bank.Transfer(); break; case 5: Program.Quit(); break; } } public static void SignUp() { Console.Clear(); Console.WriteLine("Thank you for choosing Bam bank of NSW.\nPlease enter your details to open a new account\n"); Console.WriteLine("Please enter your name"); string name = Console.ReadLine(); Console.WriteLine("Enter your Email. This will become your Username:"); string email = Console.ReadLine(); if (email == "") { Console.WriteLine("Cannot be blank."); } Console.WriteLine("Please enter your Age:"); int age = Convert.ToInt32(Console.ReadLine()); if (age < 18) { Console.WriteLine("To open an account you need to be 18yrs."); } Console.WriteLine("Please create a password:"); string password = Console.ReadLine(); if (password == "") { Console.WriteLine("Cannot be blank."); } Console.WriteLine($"Account successfully created for {email}. Please login"); Accounts accounts = new Accounts(); accounts.Name = name; accounts.Email = email; accounts.Age = age; accounts.Password = password; Accounts.AllAccounts.Add(accounts); Console.ReadKey(); Login(); } public static void Login() { Console.Clear(); Console.WriteLine("Please enter your email/username"); string uname = Console.ReadLine(); Accounts accounts = new Accounts(); if (uname == accounts.Email) { Console.WriteLine("Please enter your Password"); } else if (uname != accounts.Email) { TryAgain(); } string pword = Console.ReadLine(); if (pword == accounts.Password) { WelcomeMenu(); } else if (pword != accounts.Password) { TryAgain(); } } public static void TryAgain() { Console.Clear(); Console.WriteLine("Invalid username/Password. Please select a function.\n1: Try Again \n2: Main Menu \n3: Quit"); int input2 = Convert.ToInt32(Console.ReadLine()); switch (input2) { case 1: Login(); break; case 2: MainMenu(); break; case 3: Program.Quit(); break; } } } class Program { static void Main(string[] args) { BankAccount.MainMenu(); } public static void Quit() { Console.Clear(); Console.WriteLine("Thank you and have a nice day! \nPress any key to exit"); Console.ReadKey(); return; } }
Accounts类代码
public class Accounts { private static List<Accounts> accounts = new List<Accounts>(); public static List<Accounts> AllAccounts { get { return accounts; } } public Accounts() { } public string Name { get; set; } public string Email { get; set; } public int Age { get; set; } public string Password { get; set; } }
问题根源
登录逻辑的核心错误:Login方法里新建了一个空的Accounts对象,这个对象的Email和Password都是默认的null,根本不会和注册时存入AllAccounts列表里的用户数据匹配,所以永远无法登录成功。
修复后的登录方法
替换原来的Login方法,改为从AllAccounts列表中查找匹配邮箱的用户,再验证密码:
public static void Login() { Console.Clear(); Console.WriteLine("Please enter your email/username"); string uname = Console.ReadLine(); // 从已注册账号列表中查找匹配邮箱的用户 Accounts matchedAccount = Accounts.AllAccounts.FirstOrDefault(a => a.Email == uname); if (matchedAccount == null) { // 没找到对应邮箱的账号,进入重试 TryAgain(); return; } // 找到账号,提示输入密码 Console.WriteLine("Please enter your Password"); string pword = Console.ReadLine(); if (pword == matchedAccount.Password) { // 登录成功,跳转欢迎菜单(显示当前登录用户的名字) Console.Clear(); Console.WriteLine($"Welcome {matchedAccount.Name} \nPlease select from the following menu: \n1: View Balance \n2: Deposit \n3: Withdraw \n4: Transfer \n5: Quit"); int input1; // 做输入合法性校验,避免非数字输入崩溃 while (!int.TryParse(Console.ReadLine(), out input1)) { Console.WriteLine("Invalid input, please enter a number."); } switch (input1) { case 1: Bank.ViewBalance(); break; case 2: Bank.Deposit(); break; case 3: Bank.Withdraw(); break; case 4: Bank.Transfer(); break; case 5: Program.Quit(); break; } } else { // 密码错误,进入重试 TryAgain(); } }
额外优化建议
- 注册模块错误处理:当前注册时遇到空值/年龄不足的情况,只是输出提示但仍会创建账号,应该添加逻辑阻止无效账号创建,比如:
// 邮箱为空或全空格时,提示并重新进入注册流程 if (string.IsNullOrWhiteSpace(email)) { Console.WriteLine("Email cannot be blank or whitespace."); Console.ReadKey(); SignUp(); return; } // 年龄不足18岁时,提示并返回主菜单 if (age < 18) { Console.WriteLine("To open an account you need to be at least 18 years old."); Console.ReadKey(); MainMenu(); return; }
- 输入异常处理:所有使用
Convert.ToInt32的地方,建议改用int.TryParse,避免用户输入非数字导致程序崩溃,示例参考修复后登录方法中的输入校验逻辑。
内容的提问来源于stack exchange,提问作者Amy Douglas
相关产品推荐
相关产品推荐

