gpt4 book ai didi

c# - pig 拉丁翻译崩溃C#

转载 作者:行者123 更新时间:2023-12-03 16:46:59 24 4
gpt4 key购买 nike

由于某种原因,当我运行此代码时,我注意到字母“I或i”或单词“it”使程序崩溃。另外,当我单击“翻译”而没有输入任何内容时,它也会崩溃。我已经遍历了这段代码,但是我找不到问题。有什么建议么?

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

}


private void btnTranslate_Click(object sender, EventArgs e)
{

String input = Convert.ToString(txtInput.Text.Trim());
String inputTr = Regex.Replace(input, " {2,}", " ");
String pigLatin = "";
String temp = "";
String restOfWord = "";
String vowels = "AEIOUaeiou";
String consonants = "YBCDFGHJKLMNPQRSTVXWZbcdfghjklmnpqrstvxwzy";

string[] words = inputTr.Split();
foreach (string word in words)
{
if (string.IsNullOrEmpty(txtInput.Text))
{
MessageBox.Show("Text must be entered");
}

int index = word.IndexOfAny(new char[] { 'A', 'E', 'I', 'O', 'U', 'a', 'e', 'i', 'o', 'u' });

if (Regex.IsMatch(word, "[@#$%0-9]"))
{
pigLatin += word + " ";
}

else if (!(char.IsPunctuation(word.Last())) && vowels.Contains(word[0]) && word.Contains(word.Substring(1, 2).ToLower()))

{
pigLatin += word + "way" + " ";
}
else if (char.IsPunctuation(word.Last()) && vowels.Contains(word[0]) && word.Contains(word.Substring(1, 2).ToLower()))

{
pigLatin += word.Substring(0, word.Length - 1) + "way" + word.Last() + " ";
}
else if (!(char.IsPunctuation(word.Last())) && consonants.Contains(word[0]) && word.StartsWith(word.Substring(0, 1).ToLower()))
{
string firstPart = word.Substring(0, index);
restOfWord = word.Substring(index, word.Length - index);
pigLatin += restOfWord + firstPart + "ay" + " ";
}

else if (char.IsPunctuation(word.Last()) && consonants.Contains(word[0]) && word.StartsWith(word.Substring(0, 1).ToLower()))
{
string firstPart = word.Substring(0, index);
restOfWord = word.Substring(index, word.Length - index);
pigLatin += restOfWord.Substring(0, restOfWord.Length - 1) + firstPart + "ay" + restOfWord.Last() + " ";
}

else if (!(char.IsPunctuation(word.Last())) && word.Contains(word.ToUpper()) && vowels.Contains(word.Substring(0, 1).ToUpper()))
{

pigLatin += word + "WAY" + " ";

}
else if (char.IsPunctuation(word.Last()) && word.Contains(word.ToUpper()) && vowels.Contains(word.Substring(0, 1).ToUpper()))
{

pigLatin += word.Substring(0, word.Length - 1) + "WAY" + word.Last() + " ";

}
else if (!(char.IsPunctuation(word.Last())) && word.StartsWith(word.Substring(0, 1).ToUpper()) && consonants.Contains(word.Substring(0, 1).ToUpper()) && word.Contains(word.Substring(1, 2).ToLower()))
{
string firstPart = word.Substring(0, index);
restOfWord = word.Substring(index, word.Length - index);
pigLatin += restOfWord.Substring(0, 1).ToUpper() + restOfWord.Substring(1, restOfWord.Length - 1).ToLower() + firstPart.ToLower() + "ay" + " ";
}

else if (char.IsPunctuation(word.Last()) && word.StartsWith(word.Substring(0, 1).ToUpper()) && consonants.Contains(word.Substring(0, 1).ToUpper()) && word.Contains(word.Substring(1, 2).ToLower()))
{
string firstPart = word.Substring(0, index);
restOfWord = word.Substring(index, word.Length - index);
temp = restOfWord.Substring(0, 1).ToUpper() + restOfWord.Substring(0, restOfWord.Length - 1).ToLower() + firstPart.ToLower() + "ay" + restOfWord.Last() + " ";
temp = temp.Remove(0, 1);
pigLatin += temp.Substring(0, 1).ToUpper() + temp.Substring(1, temp.Length - 1).ToLower() + " ";
}

else if (!(char.IsPunctuation(word.Last())) && word.Contains(word.ToUpper()) && consonants.Contains(word.Substring(0, 1).ToUpper()))
{
string firstPart = word.Substring(0, index);
restOfWord = word.Substring(index, word.Length - index);
pigLatin += restOfWord.ToUpper() + firstPart.ToUpper() + "AY" + " ";
}

else if (char.IsPunctuation(word.Last()) && word.Contains(word.ToUpper()) && consonants.Contains(word.Substring(0, 1).ToUpper()))
{
string firstPart = word.Substring(0, index);
restOfWord = word.Substring(index, word.Length - index);
pigLatin += restOfWord.Substring(0, restOfWord.Length - 1).ToUpper() + firstPart.ToUpper() + "AY" + word.Last() + " ";
}
txtOutput.Text = pigLatin;
}

}

private void btnClear_Click(object sender, EventArgs e)
{
txtInput.Text = "";
txtOutput.Text = "";
txtInput.Focus();
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}

private void txtInput_TextChanged(object sender, EventArgs e)
{

}
}

最佳答案

在整个代码中的几个地方,您都有“Substring(1,2)”-如果当前正在处理的单词短于三个字符,那么您将得到一个异常,因为您试图获取的字符串超出了末尾。字符串。

您需要在代码中添加长度检查。
例如

...
...
else if (!(char.IsPunctuation(word.Last())) && vowels.Contains(word[0]) &&
&& (word.Length >= 3) && word.Contains(word.Substring(1, 2).ToLower()))
...
...

就像调试说明一样,您可以在发生异常的行上放置一个(条件)断点,然后在“即时窗口”(复制和粘贴)中检查if语句的各个部分,以查看是哪个子句导致了异常。

关于c# - pig 拉丁翻译崩溃C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42783661/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com