我是一個新手,作爲一個練習,必須創建一個應用程序,允許用戶3次嘗試正確輸入其引腳。我遇到的問題是,一旦輸入正確的引腳,應用程序就會關閉。第三次嘗試後也是如此。所以「正確的Pin」和「再見」不會先打印出來。這是我目前所擁有的。謝謝。如何在正確的引腳輸入和三次嘗試後關閉C#控制檯應用程序?
int pin = 2456;
int attempts = 1;
bool correctPin = false;
while (attempts <= 3 && correctPin == false)
{
//Ask user to enter pin
Console.WriteLine("Please enter your pin");
//read what user types
string guess = Console.ReadLine();
int number1 = int.Parse(guess);
attempts++;
//if correct pin entered
if (number1 == pin)
{
correctPin = true;
Console.WriteLine("Correct Pin");
}
//if incorrect pin entered
else if (number1 != pin)
{
Console.WriteLine("Incorrect Pin");
}
else if (attempts > 3)
{
Console.WriteLine("Goodbye");
}
}
}
}
}
謝謝!我曾嘗試過Console.ReadLine();但直到現在才意識到,它必須在我想要的方式下工作,這是合理的。 – ChunLi