简体   繁体   English

C#控制台应用程序密码输入检查器

[英]C# Console Application Password Input Checker

The following code has preset passwords that the user must enter to continue the code. 以下代码具有用户必须输入的预设密码才能继续该代码。 However, when the set passwords (PASS1-PASS3) are entered, the code goes to the do-while regardless. 但是,当输入设置的密码(PASS1-PASS3)时,无论执行什么操作,代码都会进入执行。 What do I need to do in order to make the while recognize that the password is correct so that it does not go to the invalid password line? 为了使一会儿能够识别密码正确,以便不会进入无效密码行,我该怎么办?

// Program asks user to enter password
// If password is not "home", "lady" or "mouse"
// the user must re-enter the password
using System;
public class DebugFour1
{
public static void Main(String[] args)
  {
const String PASS1 = "home";
const String PASS2 = "lady";
const String PASS3 = "mouse";
String password;
String Password;
Console.Write("Please enter your password ");
password = Console.ReadLine();
do
{
    Console.WriteLine("Invalid password enter again: ");
    password = Console.ReadLine();
} while (password != PASS1 || password != PASS2 || password != PASS3);
Console.WriteLine("Valid password");
Console.ReadKey();

 }
}

Try changing the "||" 尝试更改“ ||” to "&&". 至 ”&&”。

It will cannot equal all of them at once. 它不能一次等于所有。

You have your logic the wrong way around ie do something then check some conditions, whereas you want to check some conditions and then do something. 您有错误的逻辑处理方法,即先执行某些操作然后检查某些条件,而您想要先检查某些条件然后执行某些操作。 So the following code: 所以下面的代码:

do
{
    Console.WriteLine("Invalid password enter again: ");
    password = Console.ReadLine();
} while (password != PASS1 || password != PASS2 || password != PASS3);

Should read: 应该读:

while (password != PASS1 && password != PASS2 && password != PASS3)
{
    Console.WriteLine("Invalid password enter again: ");
    password = Console.ReadLine();
} 

Notice that I also changed the logical ORs || 请注意,我还更改了逻辑OR || to logical ANDs && . 逻辑与&& this is because you want to check if it is not equal to all of them, not just one. 这是因为您要检查它是否不等于全部,而不仅仅是一个。

On a side note the variable Password is unused and should be removed as it can lead to typo errors to your used variable password . 附带说明一下,变量Password未使用,应删除,因为它可能导致您使用的变量password出现拼写错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM