我目前在编辑列表中存储的现有用户名和密码时遇到问题。
声明:
public static Administrator Cadmin = new Administrator("", "", "", "");
public static Staff Cstaff = new Staff("", "", "", "");
public static Administrator Ladmin = new Administrator("", "", "", "");
public static Staff Lstaff = new Staff("", "", "", "");
public static string NCName;
public static string NCPassword;
List<User> UserList = new List<User>();
执行代码:
Console.WriteLine("Which user would you like to edit?");
string ruser = Console.ReadLine();
bool Ustop = false;
while (!Ustop)
{
foreach (User u in UserList)
{
if (ruser == Cadmin.CName)
{
Console.WriteLine("Please key in the existing password of the selected username");
string epass = Console.ReadLine();
if (epass == Cadmin.CPassword)
{
Console.WriteLine("Create new Administrator Username:");
NCName = Console.ReadLine();
Console.WriteLine("\nCreate new Administrator Password: ");
NCPassword = Console.ReadLine();
ruser.Replace(ruser, NCName);
epass.Replace(epass, NCPassword);
}
else
{
Console.WriteLine("Password that you key in is invalid!");
}
}
else
{
Console.WriteLine("Username that you key in did not exist!");
Console.WriteLine("Please key in a valid username");
}
}
}
答案 0 :(得分:0)
我了解到您在将数据存储到当前填充的列表中时遇到问题,因此这是一种解决方案,如果foreach则用于,然后可以使用列表的索引来编辑列表的特定成员(我认为CAdmin是从User和User类继承的属性具有CName和CPassword的属性):
bool Ustop = false;
while (!Ustop)
{
for (var i = 0; i< UserList.Count ; i++ )
{
User u = UserList[i];
if (ruser == u.CName)
{
Console.WriteLine("Please key in the existing password of the selected username");
string epass = Console.ReadLine();
if (epass == u.CPassword)
{
Console.WriteLine("Create new Administrator Username:");
NCName = Console.ReadLine();
Console.WriteLine("\nCreate new Administrator Password: ");
NCPassword = Console.ReadLine();
u.CName = NCName;
u.CPassword = NCPassword;
}
else
{
Console.WriteLine("Password that you key in is invalid!");
}
}
else
{
Console.WriteLine("Username that you key in did not exist!");
Console.WriteLine("Please key in a valid username");
}
}
}