无法隐式将类型字符转换为字符串
本文关键字:字符 转换 字符串 类型 | 更新日期: 2023-09-27 18:31:36
我创建了一个播放器类,并从该类为我的菜单驱动的播放器系统创建了一个数组 我正在尝试使用我的GetChar
方法来继续显示提示并读取用户在键盘上键入的任何内容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
方法
public static char GetChar(String prompt)
{
// declare variables
char validChar;
while (!Char.TryParse(Console.ReadLine(), out validChar))
{
Console.WriteLine("Invalid entery - try again.");
Console.Write(prompt);
}
return validChar;
}
while (!Char.TryParse(Console.ReadLine(), out validChar))
在此行中,您正在读取字符串数据,而不是单个字符,因此无法将其分配给"validChar"(方法 Console.ReadLine)。尝试将此方法的调用分配给字符串变量,然后使用索引 [] 运算符获取第一个字符。
或者,您可以使用
public static ConsoleKeyInfo ReadKey()