Winforms 中的 C# 字数组帮助

C# word array help in Winforms

所以在我的 class 中,我们正在使用 Winforms 做一个 pig latin 翻译器。如果我将公式从我的 Convert 函数复制到 click 事件,它对 1 个单词来说工作得很好。但是,否则我无法使其正常运行。

这是我目前的代码,它给我的问题是输出标签没有给我转换后的词。相反,它给我这条消息:

比如我输入'Bob' 我得到这个作为我的输出: “System.Collections.Generic.List`1[System.String]鲍勃

    private void btn_Translate_Click(object sender, EventArgs e)
    {
        string userInput = box_Input.Text;
        if (userInput.Contains(" "))
        {
            string[] words = userInput.Split(' ');
            foreach (var word in words)
            {
                Validator(word);
            }
        }
        else
        {
            Validator(userInput);
        }

    }
    public void Validator(string s)
    {
        string chkInput = s;

        if (string.IsNullOrEmpty(chkInput))
        {
            Error(1);
            Error(0);
        }
        else
        {
            if (chkInput.Length < 2)
            {
                Error(1);
                Error(0);
            }
            else
            {
                if (!chkInput.Any(x => char.IsLetter(x)))
                {
                    Convert(chkInput);
                }
                else
                {
                    Error(3);
                    Error(0);
                }
            }
        }
    }

    public void Convert(string convInput)
    {
        string word = convInput;
        string charStrNew;
        string firstLetter = word.Substring(0, 1);
        string theRest = word.Substring(1, word.Length - 1);
        string suffix = "ay";
        charStrNew = $"{theRest}{firstLetter}{suffix}";
        string listStrOutput = String.Join(" ", charStrNew);

        lbl_Output.Text = listStrOutput;
    }

您的 Validator 有误。将 !chkInput.Any(x => char.IsLetter(x)) 替换为 !chkInput.Any(x => !char.IsLetter(x)) 或更好,使用 Enumerable.AllchkInput.All(char.IsLetter)

Determines whether all elements of a sequence satisfy a condition.

msdn

public static void Main()
{
    btn_Translate_Click("bob");
}

public static void btn_Translate_Click(string userInput)
{
    if (userInput.Contains(" "))
    {
        string[] words = userInput.Split(' ');
        foreach (var word in words)
        {
            Validator(word);
        }
    }
    else
    {
        Validator(userInput);
    }

}

public static void Error(int output)
{
    Console.WriteLine(output);
}

public static void Validator(string s)
{
    string chkInput = s;

    if (string.IsNullOrEmpty(chkInput))
    {
        Error(1);
        Error(0);
    }
    else
    {
        if (chkInput.Length < 2)
        {
            Error(1);
            Error(0);
        }
        else
        {
            if (chkInput.All(char.IsLetter))
            {
                Console.WriteLine(Convert(chkInput));
            }
            else
            {
                Error(3);
                Error(0);
            }
        }
    }
}

public static string Convert(string convInput)
{
    string word = convInput;
    string charStrNew;
    string firstLetter = word.Substring(0, 1);
    string theRest = word.Substring(1, word.Length - 1);
    string suffix = "ay";
    charStrNew = $"{theRest}{firstLetter}{suffix}";
    string listStrOutput = String.Join(" ", charStrNew);

    return listStrOutput;
}

Demo