C# PigLatin翻译程序崩溃C
由于某种原因,当我运行这段代码时,我注意到字母I或I或单词it使程序崩溃。此外,当我只需单击“翻译”而未输入任何内容时,它也会崩溃。我已经一遍又一遍地检查了这个代码,但是我找不到问题所在。有什么建议吗C# PigLatin翻译程序崩溃C,c#,crash,C#,Crash,由于某种原因,当我运行这段代码时,我注意到字母I或I或单词it使程序崩溃。此外,当我只需单击“翻译”而未输入任何内容时,它也会崩溃。我已经一遍又一遍地检查了这个代码,但是我找不到问题所在。有什么建议吗 public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object
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)
{
}
}
在代码中的几个地方都有子字符串1、2—如果当前处理的单词短于三个字符,则会出现异常,因为您试图获取超出字符串末尾的子字符串 您需要在代码中添加长度检查。 e、 g
正如调试注意事项一样,您可以在异常发生的行上放置一个条件断点,然后在即时窗口“复制和粘贴”中检查if语句的每个单独部分,以查看导致异常的子句。Crash=exception。你必须告诉我们哪一个和哪一行。它有什么例外?获取调用堆栈?InvalidOperationException发生异常引发:“System.Core.dll@CallumBradburyelse if!中的System.InvalidOperationException!”!char.ispuncationword.Last&&vouels.containsSwarm[0]&&word.containsSwarm.Substring1,2.ToLower@Sinatr它位于highlights@KenYoung,您可以将问题添加到所需的相关详细信息中:完整的异常文本,包括消息和调用堆栈,以及标记代码行,例如注释say//here。加上if string.IsNullOrEmptytxtInput.text应放在方法的开头,foreach之外,加上txtOutput.text=pigLatin;应该放在foreach之后…@PaulF我已经搞了10个小时了。。。你能帮我添加长度检查代码吗?我理解你对子字符串的意思。对于延迟,我很抱歉-我不确定你在检查2个字符的子字符串时要做什么,但简单的解决方法是在尝试获取子字符串之前在if语句中插入word.Length>=3-请参见上面的编辑。
...
...
else if (!(char.IsPunctuation(word.Last())) && vowels.Contains(word[0]) &&
&& (word.Length >= 3) && word.Contains(word.Substring(1, 2).ToLower()))
...
...