2016-11-07 5 views
0

私はいくつかの宿題をしています。私は立ち往生しました。ほとんどすべてが終わりに2行を除いて正しく動作しているようです、私は束の周りのものを切り替えて、Heyは無効な引数を持っています

宿題の質問は

デザイン、文字列の入力を要求し、その後、モールスコードにその文字列を変換するプログラムです。モールス符号は、英語アルファベットの各文字、各数字、および様々な句読点文字が一連の点線およびダッシュで表されるコードである。表8-7にコードの一部を示します。

私が知る限り、辞書の項目はすべての文字を正しく変換する必要がありますが、無効な引数のためにプログラムを実行することはできません。私は最後に、私のコピー誰かが本当に感謝してくれるなら、コードは以下の通りです。私はそれが無効な引数をどこに示しているのかをコメントします。任意のヒントや右方向にプッシュを大幅にあなたがここにDictionary<char, string>を使用して、単一引用符に.Addコールであなたの最初の引数を変更する必要があり

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

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

    private void convertbtn_Click(object sender, EventArgs e) 
    { 
     string input = userinput.Text.Trim(); 
     outputlabel.Text = mcodeconv(input); 
    } 
    private string mcodeconv(string input) 
    { 
     string codeConvert = string.Empty; 
     Dictionary<string, string> newmorse = new Dictionary<string, string>  (); 
     { 
      newmorse.Add(" " , "space"); 
      newmorse.Add("," , "--..--"); 
      newmorse.Add("." , ".-.-.-"); 
      newmorse.Add("?" , "..--.."); 
      newmorse.Add("0" , "-----"); 
      newmorse.Add("1" , ".----"); 
      newmorse.Add("2" , "..---"); 
      newmorse.Add("3" , "...--"); 
      newmorse.Add("4" , "....-"); 
      newmorse.Add("5" , "....."); 
      newmorse.Add("6" , "-...."); 
      newmorse.Add("7" , "--..."); 
      newmorse.Add("8" , "---.."); 
      newmorse.Add("9" , "----."); 
      newmorse.Add("A" , ".-"); 
      newmorse.Add("B" , "-..."); 
      newmorse.Add("C" , "-.-."); 
      newmorse.Add("D" , "-.."); 
      newmorse.Add("E" , "."); 
      newmorse.Add("F" , "..-."); 
      newmorse.Add("G" , "--."); 
      newmorse.Add("H" , "...."); 
      newmorse.Add("I" , ".."); 
      newmorse.Add("J" , ".---"); 
      newmorse.Add("K" , "-.-"); 
      newmorse.Add("L" , ".-.."); 
      newmorse.Add("M" , "- --"); 
      newmorse.Add("N" , "-."); 
      newmorse.Add("O" , "---"); 
      newmorse.Add("P" , ".--."); 
      newmorse.Add("Q" , "--.-"); 
      newmorse.Add("R" , ".-."); 
      newmorse.Add("S" , "…"); 
      newmorse.Add("T" , "-"); 
      newmorse.Add("U" , "..-"); 
      newmorse.Add("V" , "...-"); 
      newmorse.Add("W" , ".--"); 
      newmorse.Add("X" , "-..-"); 
      newmorse.Add("Y" , "-.--"); 
      newmorse.Add("Z" , "--.."); 
      KeyValuePair<string, string> newvalue = new KeyValuePair<string,  string>(); 
     }; 
     char newChar; 
     foreach (char ex in input) 
     { 
      newChar = char.ToUpper(ex); 
      if(newmorse.ContainsKey(newChar)) //it throws the error here 
      { 
       codeConvert += newmorse[newChar]; //it throws the error here 
      } 
      else 
      { 
       codeConvert += ex; 
      } 
     } 
     return codeConvert; 
    } 
    private void exitbtn_Click(object sender, EventArgs e) 
    { 

    } 
} 
} 
+0

あなた 'newmorse'辞書は'辞書<文字、文字列> 'でなければなりません。次に、それぞれのキーについて、一重引用符で囲みます: 'newmorse.Add( 'A'、...); newmorse.Add( 'B'、...); '。現在、キーが文字列である辞書内の文字を検索しようとしています。一致するデータ型が必要です。 –

+0

コーリーのコメントとニヨコの答えは正しいです。文字列をcharと比較していますが、これは動作しません。 charを文字列として作成するか、辞書のキーをcharとして作成します。 – Everyone

答えて

4

をいただければ幸いです。あなたのエラーはchar引数を使用してstringキーでDictionaryを検索したためです。例えば

Dictionary<char, string> newmorse = new Dictionary<char, string>(); 

newmorse.Add(' ' , "space"); 
newmorse.Add(',' , "--..--"); 
newmorse.Add('.' , ".-.-.-"); 
newmorse.Add('?' , "..--.."); 

// etc 
関連する問題