0
我创建了一个玩家类并为该菜单驱动的玩家系统创建了一个数组我试图用我的GetChar
方法继续显示提示符并阅读用户在键盘上键入的内容直到Char.TryParse
可以将输入转换成字符,但我不断收到错误不能将类型字符转换为字符串
无法含蓄转换char类型为字符串
,当我打电话给我的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;
}