【发布时间】:2021-08-21 21:29:17
【问题描述】:
这是我的代码
Console.WriteLine("Do you want to: 1. play against an Ai or 2. let two Ai´s play aigainst each other?");
Console.WriteLine("Please choose one option!");
int userInput = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("\n");
if (userInput == 1)
{
// Player
Player player = new Player();
我尝试创建一个 try catch 块,但在我的 if 语句中总是遇到 userInput 的问题。我想要一个try .. catch 块来确保,如果用户输入一个字符或其他东西(+,~,#,...)他们会收到一个错误消息并且可以输入新内容。
【问题讨论】:
-
int.TryParse和一个循环?