以下代碼具有用戶必須輸入的預設密碼才能繼續執行代碼。但是,如果輸入了設定的密碼(PASS1-PASS3),則代碼將轉到do-while而不管。我需要做些什麼才能讓認識到密碼是正確的,以便它不會進入無效的密碼行?C#控制檯應用程序密碼輸入檢查器
// 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);''你不需要做while循環。 while循環工作。 –