由于某些原因,当我运行此代码时,我注意到这封信"我或我"或者说"它"崩溃程序。此外,当我点击翻译时没有输入任何内容也会崩溃。我一遍又一遍地看过这段代码,但我找不到问题。有什么建议?
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)
{
}
}
答案 0 :(得分:1)
在整个代码中的几个地方你有“子串(1,2)” - 如果你当前处理的单词长度超过三个字符,那么你会得到一个例外,因为你试图获得一个超出范围的子串字符串的结尾。
您需要在代码中添加长度检查。 e.g。
...
...
else if (!(char.IsPunctuation(word.Last())) && vowels.Contains(word[0]) &&
&& (word.Length >= 3) && word.Contains(word.Substring(1, 2).ToLower()))
...
...
正如关于调试的注释 - 你可以在发生异常的行上放置一个(条件)断点&然后在立即窗口(复制和粘贴)中检查if语句的每个部分,以查看导致异常的子句。