我希望程序要求用户一次又一次地输入数字,直到格式异常不存在.
static void Main(string[] args)
{
int userGuess1 = 0;
int userGuess2 = 0;
do
{
try
{
Console.WriteLine("Welcome to the game. To win User 2 must guess the number User 1 inputs!");
Console.WriteLine("User 1, Guess a number between 1 and 10 : ");
userGuess1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("User 2, Guess a number between 1 and 10 : ");
userGuess2 = Convert.ToInt32(Console.ReadLine());
if (userGuess2 < userGuess1)
{
Console.WriteLine("Your guess is too low.");
}
else if (userGuess2 > userGuess1)
{
Console.WriteLine("Your guess is too high.");
}
}
catch (FormatException e)
{
Console.WriteLine("Please enter numbers between 1 and 10 only.");
Console.ReadKey();
}
}
while (userGuess1 != userGuess2);
Console.WriteLine("Congratulations! You won!");
Console.ReadKey();
}
我无法让程序再次循环.当出现格式异常时,它将退出.我try 移动try和catch语句,但它会产生错误.