所以我有一个名为'Inlogpoging'的登录尝试循环。 3次后,它需要给你“登录限制”的消息。但是我最终在剧本中遇到错误,这有什么不对?
static void Main(string[] args)
{
for (int inlogpoging = 0; inlogpoging < 3; inlogpoging++)
{
Console.WriteLine("Status: " + status.Onaangemeld);
Console.Write("Gebruikersnaam:");
string Naam = Console.ReadLine();
Console.Write("Wachtwoord:");
string Wachtwoord = Console.ReadLine();
if (Naam == "administrator" && Wachtwoord == "SHARPSOUND")
{
Console.Clear();
Console.WriteLine("Status: " + status.Ingelogd);
Console.WriteLine("Welkom bij SoundSharp {0}!", Naam);
Console.ReadLine();
break;
}
Console.Clear();
Console.WriteLine("Helaas, gebruikersnaam of wachtwoord niet correct.");
}
if (int inlogpoging == 3); //Right here <--
{
Console.Clear();
Console.WriteLine("Login limited.");
}
}
答案 0 :(得分:2)
if
附近有语法错误; if
内不允许声明,这些声明用于评估条件。并且无需使用;
终止它们,以下代码可以帮助您:
int inlogpoging = 0;
for (; inlogpoging < 3; inlogpoging++)
{
// do the operations here
}
// change the if as below
if (inlogpoging == 3)
{
Console.Clear();
Console.WriteLine("Login limited.");
}
答案 1 :(得分:0)
我认为你的代码没有做你想要的。 考虑下面的代码,检查评论:
static void Main(string[] args)
{
int inlogpoging = 0;//initialize int counter
while(inlogpoging<3)
{
if (inlogpoging == 3) //if equal to 3 then stop loguin process
{
Console.Clear();
Console.WriteLine("Login limited.");
}
else {//if not 3 then process loguin
Console.WriteLine("Status: " + status.Onaangemeld);
Console.Write("Gebruikersnaam:");
string Naam = Console.ReadLine();
Console.Write("Wachtwoord:");
string Wachtwoord = Console.ReadLine();
//at this point increment counter
inlogpoging++;
if (Naam == "administrator" && Wachtwoord == "SHARPSOUND")
{
Console.Clear();
Console.WriteLine("Status: " + status.Ingelogd);
Console.WriteLine("Welkom bij SoundSharp {0}!", Naam);
Console.ReadLine();
Console.Clear();
Console.WriteLine("Helaas, gebruikersnaam of wachtwoord niet correct.");
//need a exit point
return 0;
}
}
}
}