C#如何从循环中删除最后一行
我写了一个运行良好的代码。 然而,有一些事情让我恼火。 这是密码C#如何从循环中删除最后一行,c#,for-loop,C#,For Loop,我写了一个运行良好的代码。 然而,有一些事情让我恼火。 这是密码 string secretWord = "ola"; string guess; int counter = 4; Console.WriteLine("Try to guess a secret word that contain 3 character. You have 4 attemps"); for (int i = 0; i<3;
string secretWord = "ola";
string guess;
int counter = 4;
Console.WriteLine("Try to guess a secret word that contain 3 character. You have 4 attemps");
for (int i = 0; i<3; i++)
{
guess = Console.ReadLine();
if (guess == secretWord)
{
Console.WriteLine("Well done! You guessed correctly");
break;
}
else if (guess.Length != 3)
{
counter--;
Console.WriteLine("Only three characters. You lost one attempt");
Console.WriteLine("You have now {0} attempts", counter);
}
else if (guess != secretWord)
{
counter--;
Console.WriteLine("Wrong. you have now {0} attempts", counter);
}
if(counter == 0)
{
Console.WriteLine("Bad lucky. The secret word is {0}", secretWord);
}
}
string secretWord=“ola”;
字符串猜测;
int计数器=4;
WriteLine(“试着猜一个包含3个字符的秘密单词,你有4个尝试”);
对于(int i=0;i您可以通过这种方式更改最后一个,否则
else if (guess != secretWord)
{
counter--;
if(counter == 0)
{
Console.WriteLine("Bad lucky. The secret word is {0}", secretWord);
break;
}
Console.WriteLine("Wrong. you have now {0} attempts", counter);
}
在按照Stefano的建议恢复顺序并更改最后一个else(如果i)之后,可以解决我的问题。实际上for循环中的是(int i=0;i