2016-07-19 31 views
0

所以我只是在使用控制台创建一个小程序,根据你输入的“密码”显示你的东西。所以当你输入密码POKEMON时,它会将你带到一个数据库。但是我希望如果你输入类似PASSWORD的东西,那么它将把你带到另一个数据库。我不知道该怎么做,我希望这会像说“如果他们输入POKEMON带他们进来,如果他们输入密码带他们到这里”一样容易,但我真的有麻烦。感谢帮助。使用ReadKey有多个“密码”?

namespace Films 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      Console.WriteLine("Hello Jake. Security check: Please type the security code for a database."); 
      while (Console.ReadKey().Key != ConsoleKey.P) {} 
      while (Console.ReadKey().Key != ConsoleKey.O) {} 
      while (Console.ReadKey().Key != ConsoleKey.K) {} 
      while (Console.ReadKey().Key != ConsoleKey.E) {} 
      while (Console.ReadKey().Key != ConsoleKey.M) {} 
      while (Console.ReadKey().Key != ConsoleKey.O) {} 
      while (Console.ReadKey().Key != ConsoleKey.N) {} 
      { 
       Console.WriteLine("\n Passcode entered successfully."); 
       while (Console.ReadKey().Key != ConsoleKey.O) { } 
       while (Console.ReadKey().Key != ConsoleKey.K) { } 
       { 
        Console.WriteLine("\n \n      Loading Database"); 
        Console.WriteLine("\n"); 
        Console.WriteLine("Test"); 
        Console.ReadKey(); 
       } 
      } 
     } 
    } 
} 
+1

到Console.ReadLine()返回所有用户敲击回车键之前输入,这似乎是一个更好的方式来做到这个。然后,您可以使用switch()检查文本并执行所需的操作。 –

+0

烘焙在信誉是微不足道的从您的可执行文件中提取,因此毫无意义。 – spender

回答

1

有一个名为Console.ReadLine()

ReadLine方法读取从标准输入流的线方法。

如果标准输入设备是键盘,ReadLine方法会阻止,直到用户按下Enter键。

在你的情况,你会使用这样的:

Console.WriteLine("Hello Jake. Security check: Please type the security code for a database."); 

// This will read all the characters until the user presses Enter 
string password = Console.ReadLine(); 

switch (password) 
{ 
    case "POKEMON": 
    { 
     // Load some database 
     break; 
    } 
    case "PASSWORD": 
    { 
     // Load another database 
     break; 
    } 
    default: 
    { 
     Console.WriteLine("Invalid password"); 
    } 
} 
+0

工作就像一个魅力!非常感谢你! :d –