我为我的菜单驱动的播放器系统创建了一个播放器类并从该类创建了一个数组我试图使用我的GetChar
方法继续显示提示并读取用户在键盘上键入的内容,直到{{ 1}}可以将输入转换为char但是我一直收到错误
无法将char类型转换为字符串
当我调用Char.TryParse
方法时。
任何帮助将不胜感激
GetChar
这是我的// Creates a player in the tables if the array is not already full and the name is not a duplicate
static void ProcessCreate(Int32 number, String firstName, String lastName, Int32 goals,
Int32 assists, Player[] players, ref Int32 playerCount, Int32 MAXPLAYERS)
{
//Int32 player = 0;
if (playerCount < MAXPLAYERS)
{
number = IOConsole.GetInt32("\nCreate Player: please enter the player's number");
//Console.ReadLine();
if (GetPlayerIndex(number, firstName, lastName, goals, assists, players, ref playerCount) == -1)
{
firstName = IOConsole.GetChar("\nCreate Player: please enter the player's First Name");
//Console.ReadLine();
lastName = IOConsole.GetChar("\nCreate Player: please enter the player's Last Name");
//Console.ReadLine();
goals = IOConsole.GetInt32("\nCreate Player: please enter the player's goals");
Int32.Parse(Console.ReadLine());
assists = IOConsole.GetInt32("\nCreate Player: please enter the player's assists");
//Console.ReadLine();
InsertPlayer(number, firstName, lastName, goals, assists, players, ref playerCount);
Console.WriteLine("\n{0,7} {1,-20}{2, -20}{3,8}{4,8}{5,8}\n", "Number", "First Name", "Last Name", "Goals", " Assists", "Points");
for (Int32 player = 0; player < playerCount; player++)
Console.WriteLine("{0,7} {1,-20}{2, -20}{3,8}{4,8}{5,8}",
players[player].Number, players[player].FirstName, players[player].LastName,
players[player].Goals, players[player].Assists, players[player].Points());
Console.WriteLine();
}
else
Console.WriteLine("\nCreate Player: the player number already exists");
}
else
Console.WriteLine("\nCreate Player: the player roster is already full");
}
方法
GetChar
答案 0 :(得分:2)
while (!Char.TryParse(Console.ReadLine(), out validChar))
在这一行中,您正在读取字符串数据,而不是单个字符,因此您无法将其分配给&#39; validChar&#39; (方法Console.ReadLine)。 尝试将此方法的调用分配给字符串变量,然后使用index []运算符获取第一个字符。
或者你可以使用
public static ConsoleKeyInfo ReadKey()