【发布时间】: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 为我找到了它。