我试图创建两个名为用户名和密码的字符串变量,并返回一个名为“authentiated”的 bool 值。我试图将用户名和密码字符串与私有(private)字典的内容进行比较。如果用户名和密码匹配,我希望将 bool 值设置为 true。然而,我是 C# 新手,真的不知道该去哪里。任何帮助,将不胜感激。以下是我已经拥有的。 `
private Dictionary<string, string> dictionary = new Dictionary<string, string>();
public Authenticator()
{
dictionary.Add("username1", "password1");
dictionary.Add("username2", "password2");
dictionary.Add("username3", "password3");
dictionary.Add("username4", "password4");
dictionary.Add("username5", "password5");
}
public Boolean Authenticate(Boolean authenticated)
{
//get user input
Console.WriteLine("Please enter a username");
string inputUsername = Console.ReadLine();
var auth1 = from entry in dictionary
where entry.Key == " ";
Console.WriteLine("Please enter your password");
string inputPassword = Console.ReadLine();
var auth2 = from entry in dictionary
where entry.Value == " ";
`
最佳答案
试试这个:
public bool Authenticate()
{
Console.WriteLine("Please enter a username");
string inputUsername = Console.ReadLine();
Console.WriteLine("Please enter your password");
string inputPassword = Console.ReadLine();
return dictionary.ContainsKey(inputUsername) && dictionary[inputUsername] == inputPassword;
}
关于c# - 两个字符串变量与返回 bool 值的私有(private)字典进行比较c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33279676/