【发布时间】:2017-03-14 16:07:35
【问题描述】:
以下代码具有预设密码,用户必须输入才能继续代码。但是,当输入设置的密码 (PASS1-PASS3) 时,不管怎样,代码都会转到 do-while。我需要做什么才能让 while 识别密码正确,以免进入无效密码行?
// 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();
}
}
【问题讨论】:
-
用逻辑 AND && 替换您的 While 循环。
while (password != PASS1 && password != PASS2 && password != PASS3);这里不需要 do-while 循环。 while 循环有效。
标签: c# console-application password-checker