如何使用字符串char.TryParse(How to use char.TryParse with a string)

编程入门 行业动态 更新时间:2024-10-28 16:25:23
如何使用字符串char.TryParse(How to use char.TryParse with a string)

我创建了一个播放器类,并从该类为我的菜单驱动的播放器系统制作了一个数组我试图使用我的GetChar方法继续显示提示并读取键盘上用户输入的内容,直到Char.TryParse可以将输入转换为char但我一直得到错误当我调用我的GetChar方法时,不能隐式地将类型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) { string message; //Int32 player = 0; if (playerCount < MAXPLAYERS) { try { message = ("\nCreate Player: please enter the player's number"); number = IOConsole.GetInt32(message); //(Console.ReadLine()); } catch (Exception) { Console.WriteLine("Number Must Be Postive"); } if (GetPlayerIndex(number, firstName, lastName, goals, assists, players, ref playerCount) == -1) { message =("\nCreate Player: please enter the player's First Name"); firstName = IOConsole.GetChar(message); //Console.ReadLine(); message = ("\nCreate Player: please enter the player's Last Name"); lastName = IOConsole.GetChar(message); //Console.ReadLine(); message =("\nCreate Player: please enter the player's goals"); try { goals = IOConsole.GetInt32(message); //Int32.Parse(Console.ReadLine()); message = ("\nCreate Player: please enter the player's assists"); assists = IOConsole.GetInt32(message); //Console.ReadLine(); } catch (Exception) { Console.WriteLine("Number Must Be Postive"); } 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) { char validChar; var input = Console.ReadKey(); while (!Char.TryParse(input.Key.ToString(), out validChar)) { Console.WriteLine("Invalid entry - try again."); Console.Write(prompt); } return validChar; }

I created a player class and made a array from that class for my menu driven player system I am trying to use my GetChar method to keep displaying the prompt and reading whatever the user typed on the keyboard until Char.TryParse can convert the input to an char But I keep getting the error Cannot implicitly convert type char to string when I call my GetChar method and I would like to be able to use GetChar with my user input

Any help would be appreciated

//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) { string message; //Int32 player = 0; if (playerCount < MAXPLAYERS) { try { message = ("\nCreate Player: please enter the player's number"); number = IOConsole.GetInt32(message); //(Console.ReadLine()); } catch (Exception) { Console.WriteLine("Number Must Be Postive"); } if (GetPlayerIndex(number, firstName, lastName, goals, assists, players, ref playerCount) == -1) { message =("\nCreate Player: please enter the player's First Name"); firstName = IOConsole.GetChar(message); //Console.ReadLine(); message = ("\nCreate Player: please enter the player's Last Name"); lastName = IOConsole.GetChar(message); //Console.ReadLine(); message =("\nCreate Player: please enter the player's goals"); try { goals = IOConsole.GetInt32(message); //Int32.Parse(Console.ReadLine()); message = ("\nCreate Player: please enter the player's assists"); assists = IOConsole.GetInt32(message); //Console.ReadLine(); } catch (Exception) { Console.WriteLine("Number Must Be Postive"); } 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"); }

Here is my GetChar method

public static char GetChar(string prompt) { char validChar; var input = Console.ReadKey(); while (!Char.TryParse(input.Key.ToString(), out validChar)) { Console.WriteLine("Invalid entry - try again."); Console.Write(prompt); } return validChar; }

最满意答案

看看你的代码实现,你似乎专门调用GetChar函数来获取字符串输入。 但是,您的功能一次只能返回1个字符。 当您尝试将char直接转换为字符串时,您会收到该错误。 我相信这不是你想要的。 如果您使用以下内容替换GetChar代码,那应该适合您:

public static string GetString(string prompt) { var input = Console.ReadLine(); while (string.IsNullOrWhiteSpace(input)) { // you may also want to do other input validations here Console.WriteLine("Invalid entry - try again."); Console.Write(prompt); input = Console.ReadLine(); } return input; }

Looking at your code implementation, it seems that you are calling GetChar function specifically to get the string inputs. However, your function is only able to return 1 character at a time. And as you are trying to cast the char directly to a string, you are getting that error. I am sure this is not what you wanted. If you replace your GetChar code with the following, that should work for you:

public static string GetString(string prompt) { var input = Console.ReadLine(); while (string.IsNullOrWhiteSpace(input)) { // you may also want to do other input validations here Console.WriteLine("Invalid entry - try again."); Console.Write(prompt); input = Console.ReadLine(); } return input; }

更多推荐

本文发布于:2023-08-03 14:55:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1389240.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   如何使用   char   TryParse   string

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!