【问题标题】:C# word array help in WinformsWinforms 中的 C# 单词数组帮助
【发布时间】:2021-12-22 15:59:57
【问题描述】:

所以在我的课堂上,我们正在使用 Winforms 进行猪拉丁语翻译。如果我将公式从我的 Convert 函数复制到 click 事件,它对于 1 个单词非常有效。但是,否则我无法使其正常运行。

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

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

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

【问题讨论】:

  • 代码似乎没问题。你从哪里得到异常?在哪一行? dotnetfiddle.net/i3foSQ
  • 它永远不会抛出错误。它能够处理数据,只是处理方式错误。 @aloisdg 为我找到了它。

标签: c# arrays winforms


【解决方案1】:

您的Validator 中有错误。将!chkInput.Any(x =&gt; char.IsLetter(x)) 替换为!chkInput.Any(x =&gt; !char.IsLetter(x)) 甚至更好,使用Enumerable.AllchkInput.All(char.IsLetter)

判断一个序列的所有元素是否满足一个条件。

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

【讨论】:

  • 天哪,谢谢。伙计,我已经开始这样做了几个小时,并没有发现任何问题。显然我只是需要一些新鲜的眼睛。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多