它有无效参数

本文关键字:参数 无效 | 更新日期: 2023-09-27 18:16:31

我正在做一些作业,我卡住了,几乎一切似乎都是正确的,除了最后的2行,我已经切换了一堆东西,我只是似乎不能得到它的权利。

作业问题是

设计一个程序,要求用户输入一个字符串,然后将该字符串转换为莫尔斯电码。摩尔斯电码是一种用一系列点和破折号表示英语字母表中的每个字母、每个数字和各种标点符号的电码。表8-7显示了部分代码。

据我所知,我所拥有的字典条目应该正确地转换所有字符,但我无法让程序甚至运行,因为我猜我在最后有无效的参数,下面是我的代码的副本,如果有人能帮助我,我真的很感激。我将注释显示无效参数的地方。任何提示或推动在正确的方向将不胜感激

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

它有无效参数

您应该在这里使用Dictionary<char, string>并将.Add调用中的第一个参数更改为单引号。您的错误是因为您使用char参数使用string键查找Dictionary

例如:

Dictionary<char, string> newmorse = new Dictionary<char, string>();
newmorse.Add(' ' , "space");
newmorse.Add(',' , "--..--");
newmorse.Add('.' , ".-.-.-");
newmorse.Add('?' , "..--..");
// etc