用while语句实现用户登陆程序
用while实现要求用户一直输入用户名和密码 只要用户名不是admin 或者密码不是888888就一直输入,如果正确就提示成功.
方法一:
bool b = true; string userName, password; do { Console.WriteLine("请输入用户名:"); userName = Console.ReadLine(); Console.WriteLine("请输入密码:"); password = Console.ReadLine(); if ((userName == "admin") && (password == "888888")) { b = false; } else { Console.WriteLine("用户名或密码错误,请重新输入!"); } } while (b); Console.WriteLine("登陆成功!"); Console.ReadKey();
方法二:
string userName, password; while (true) { Console.WriteLine("请输入用户名"); userName = Console.ReadLine(); Console.WriteLine("请输入密码"); password = Console.ReadLine(); if (userName == "admin" && password == "888888") { Console.WriteLine("登陆成功!"); break; } else { Console.WriteLine("账户名或密码错误,请重新输入!"); } } Console.ReadKey();