繁体   English   中英

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

[英]C# Console Application Password Input Checker

以下代码具有用户必须输入的预设密码才能继续该代码。 但是,当输入设置的密码(PASS1-PASS3)时,无论执行什么操作,代码都会进入执行。 为了使一会儿能够识别密码正确,以便不会进入无效密码行,我该怎么办?

// 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();

 }
}

尝试更改“ ||” 至 ”&&”。

它不能一次等于所有。

您有错误的逻辑处理方法,即先执行某些操作然后检查某些条件,而您想要先检查某些条件然后执行某些操作。 所以下面的代码:

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

应该读:

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

请注意,我还更改了逻辑OR || 逻辑与&& 这是因为您要检查它是否不等于全部,而不仅仅是一个。

附带说明一下,变量Password未使用,应删除,因为它可能导致您使用的变量password出现拼写错误。

暂无
暂无

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

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