-1

我如何告诉程序只有在我输入正确的密码时才授予访问权限?

谢谢你。

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.");

        }

    }
}

}

4

3 回答 3

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.");
            }
        }
    }
}
于 2013-06-16T15:01:46.707 回答
1

你快到了。

Console.ReadLine()方法读取标准输入流并将其返回为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.");

当然,这不是保证应用程序安全性的好方法。

于 2013-06-16T15:03:49.907 回答
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.");
            }
        }
    }
}
于 2013-06-16T15:04:50.467 回答