如果我输入正确的密码,我怎么能告诉程序才能访问?谢谢。
namespace Password
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Please enter password:");
Console.ReadLine();
string Password = "Test";
bool PassWordMatch;
PassWordMatch = Password == "Test";
if (PassWordMatch)
{
Console.WriteLine(" Password Match. Access Granted");
}
else
{
Console.WriteLine("Password doesn't match! Access denied.");
}
}
}
}
答案 0 :(得分:1)
您可以使用Console.ReadLine
方法返回用户输入的值,您可以将其存储在相应的变量中:
namespace Password
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Please enter password:");
string password = Console.ReadLine();
bool passWordMatch;
passWordMatch = password == "Test";
if (passWordMatch)
{
Console.WriteLine(" Password Match. Access Granted");
}
else
{
Console.WriteLine("Password doesn't match! Access denied.");
}
}
}
}
答案 1 :(得分:1)
你几乎就在那里。
Console.ReadLine()
方法读取标准输入strem并将其返回为string
。您只需要确定此方法返回新字符串的内容,并将其与测试密码进行比较。
Console.WriteLine("Please enter password:");
string input = Console.ReadLine();
bool PassWordMatch = input == "Test";
if(PassWordMatch)
Console.WriteLine(" Password Match. Access Granted");
else
Console.WriteLine("Password doesn't match! Access denied.");
当然,这不是应用程序安全性的好方法。
答案 2 :(得分:0)
您尚未将读取字符串分配给变量,因此无法进行比较。
Console.ReadLine()函数可用于从输入流中读取下一行字符,如果没有更多行可用,则返回null
。
你可以这样做:
namespace Password
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Please enter password:");
string password = Console.ReadLine(); //Assign user-entered password
bool passWordMatch;
passWordMatch = password == "Test";
if (passWordMatch)
{
Console.WriteLine(" Password Match. Access Granted");
}
else
{
Console.WriteLine("Password doesn't match! Access denied.");
}
}
}
}