所以基本上我试图编写一个控制台应用程序,它要求用户输入密码,并且只会继续要求它三次,然后停止使用'而#39;循环,但它继续循环并询问我密码,即使使用了正确的密码,并且我输入了3次以上。
const string pass = "Password";
string attempt;
do
{
Console.Write("Please enter password: ");
attempt = Console.ReadLine();
if (attempt == pass)
{
Console.WriteLine("Access granted.");
}
else
{
Console.WriteLine("Access denied.");
}
} while (true);
答案 0 :(得分:2)
const string pass = "Password";
string attempt;
int n = 0;
do
{
Console.Write("Please enter password: ");
attempt = Console.ReadLine();
if (attempt == pass)
{
Console.WriteLine("Access granted.");
break;
}
else
{
Console.WriteLine("Access denied.");
n++;
}
} while (n<=3);
答案 1 :(得分:1)
因此,您需要三次询问用户密码,直到输入正确的密码并停止询问用户是否正确输入。你可以通过for循环来实现
const string pass = "Password";
string attempt;
for(int i=0;i<3;i++)
{
Console.Write("Please enter password: ");
attempt = Console.ReadLine();
if (attempt == pass)
{
Console.WriteLine("Access granted.");
i = 4;
}
else
{
Console.WriteLine("Access denied.");
}
};
答案 2 :(得分:0)
您可以执行以下操作:
const string pass = "Password";
string attempt;
int attempt = 0;
do
{
Console.Write("Please enter password: ");
attempt = Console.ReadLine();
if (attempt == pass)
{
Console.WriteLine("Access granted.");
}
else
{
Console.WriteLine("Access denied.");
attempt++;
}
} while (attempt <= 3 && attempt != pass);
答案 3 :(得分:0)
string pass = "";
while (pass != "password")
{
Console.WriteLine("enter your password here");
pass = Convert.ToString(Console.ReadLine());
if (pass == "password")
{
Console.WriteLine("your password is correct");
}
}
Console.ReadKey();