不能共谋将char类型转换为string [英] Cannot complicity convert type char to string

查看:77
本文介绍了不能共谋将char类型转换为string的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为我的菜单驱动的播放器系统创建了一个播放器类并从该类创建了一个数组我试图使用我的GetChar方法继续显示提示并读取键盘上用户输入的内容,直到Char.TryParse可以转换为输入到char但是我一直得到错误当我调用我的GetChar方法时,不能将类型char转换为字符串。



任何帮助将不胜感激



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 complicity convert type char to string when I call my GetChar method.

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)
      {
          //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");

      }





这是我的get char方法



Here is my get char method

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;
        }

推荐答案

为什么不使用 Console.ReadKey [ ^ ]而不是Console.Read?



Why don't you use Console.ReadKey[^] instead of Console.Read?

public static char GetChar(string prompt)
{

    // declare variables
    char validChar;

    var input = Console.ReadLine();

    while (!Char.TryParse(input.Key.ToString(), out validChar))
    {
        Console.WriteLine("Invalid entry - try again.");

        Console.Write(prompt);
    }

    return validChar;
}


这篇关于不能共谋将char类型转换为string的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆