2017-03-14 4 views
0

このコードを実行すると何らかの理由で文字「I or i」または「it it」がプログラムをクラッシュさせることに気付いています。また、何も入力せずにtranslateをクリックするだけで、クラッシュすることもあります。私はこのコードを何度も繰り返してきましたが、私は問題を見つけることができません。助言がありますか?あなたのコード全体にいくつかの場所でPigLatin Translator C#

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) 
    { 

    } 
} 
+2

クラッシュ=例外。あなたはどのラインにどのラインを教えなければなりません。 – Sinatr

+0

どのような例外がありますか?コールスタックを取得しましたか? !((char.IsPunctuation(word.Last()))&& vowels.Contains(ワード[0])場合System.Core.dll @CallumBradbury他 –

+0

と、InvalidOperationExceptionがスロー 例外を発生しました @Sinatr そのコード行には、 –

答えて

1

あなたは、「サブストリング(1、2)」を持っている - あなたが現在処理している言葉は、あなたが取得しようとしているので、長い間、あなたは、例外が発生します3つの文字より短い場合文字列の終わりを超えて延びる部分文字列。

コードに長さのチェックを追加する必要があります。例: ただ、デバッグのノートとして

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

- あなたは例外が&が、その後見て(&ペーストをコピー)イミディエイトウィンドウであなたのif文の個々の部分をチェック起こるライン上(条件付き)ブレークポイントを置くことができますこの句によって例外が発生しています。

+1

'foreach'の外にメソッドの先頭に' if(string.IsNullOrEmpty(txtInput.Text)) 'を加え、' txtOutput.Text = pigLatin; 'を' foreach'の後ろに置くべきです。 ... – xanatos

+0

@PaulF私はこれを約10時間今やこんできました...長さチェック用のコードを追加することで助けてくれますか?私はあなたが部分文字列で何を意味するのか理解しています。 –

+0

遅れて申し訳ありません - 2文字の部分文字列のチェックで何をしようとしているのかよく分かりませんが、単純な解決策はif文に(word.Length> = 3)を挿入することです部分文字列 - 上記の編集を参照してください。 – PaulF

関連する問題