Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 他有无效的论点_C#_Visual C# Express 2010 - Fatal编程技术网

C# 他有无效的论点

C# 他有无效的论点,c#,visual-c#-express-2010,C#,Visual C# Express 2010,我正在做一些家庭作业,我被卡住了,几乎所有的东西似乎都正常工作,除了结尾的两行,我已经把东西换了很多,我就是不能把它做好 家庭作业的问题是 设计一个程序,要求用户输入一个字符串,然后将该字符串转换为莫尔斯电码。摩尔斯电码是一种电码,其中英文字母表中的每个字母、每个数字和各种标点符号都由一系列点和破折号表示。表8-7显示了部分代码 据我所知,我拥有的字典条目应该能正确转换所有字符,但我甚至无法让程序运行,因为我猜我最后的参数是无效的,下面是我的代码副本,如果有人能帮助我,我将不胜感激。我将评论它显

我正在做一些家庭作业,我被卡住了,几乎所有的东西似乎都正常工作,除了结尾的两行,我已经把东西换了很多,我就是不能把它做好

家庭作业的问题是

设计一个程序,要求用户输入一个字符串,然后将该字符串转换为莫尔斯电码。摩尔斯电码是一种电码,其中英文字母表中的每个字母、每个数字和各种标点符号都由一系列点和破折号表示。表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)
    {

    }
}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
名称空间莫尔斯电码
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
私有void convertbtn_单击(对象发送方,事件参数e)
{
字符串输入=userinput.Text.Trim();
outputlabel.Text=mcodeconv(输入);
}
私有字符串mcodeconv(字符串输入)
{
string codeConvert=string.Empty;
字典newmorse=新字典();
{
添加(“,”空格“);
添加(“,”,“--…”);
新增(“.”,“-”);
新增(“?”,“.--…”);
加上(“0”,“----”;
加上(“1”,“----”;
加上(“2”,“.-”);
新增(“3”、“…”——”;
新增(“4”、“…-”);
新增(“5”、“…”);
新增(“6”、“-……”);
加上(“7”、“…”);
新增(“8”、“--…”);
加上(“9”,“----”);
加上(“A”、“-”);
加上(“B”、“-…”;
加上(“C”和“-.-”;
新增(“D”和“-…”);
加上(“E”,”;
新增(“F”、“.-”);
新增(“G”和“-”);
添加(“H”、“…”);
加上(“I“,”);
加上(“J”、“--”);
加上(“K”、“-.-”;
添加(“L”和“-”);
加上(“M”,“-”);
加上(“N”,“-”);
加上(“O”和“--”);
添加(“P”、“-”);
加上(“Q”、“--.-”;
新增(“R”、“-”);
新增(“S”和“…”);
加上(“T”,“-”);
新增(“U”、“.-”);
新增(“V”、“…-”);
添加(“W”、“--”);
新增(“X”、“-…-”);
加上(“Y”和“-.-”);
加上(“Z”、“--”);
KeyValuePair newvalue=新的KeyValuePair();
};
char-newChar;
foreach(输入中的字符ex)
{
newChar=char.ToUpper(ex);
if(newmorse.ContainsKey(newChar))//它在这里抛出错误
{
codeConvert+=newmorse[newChar];//它在这里抛出错误
}
其他的
{
编码转换+=ex;
}
}
返回编码转换;
}
私有void exitbtn_单击(对象发送方,事件参数e)
{
}
}
}

您应该在此处使用
字典
,并更改
中的第一个参数。将
调用添加到单引号中。您的错误是因为您使用
char
参数使用
string
键查找
Dictionary

例如:

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

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

// etc
Dictionary newmorse=newdictionary();
新增(“”,“空格”);
newmorse.Add(“,”,“--…”);
加上(“.”、“-”);
加上(“?”,“.-”);
//等

您应该在此处使用
字典
,并更改
中的第一个参数。将
调用添加到单引号中。您的错误是因为您使用
char
参数使用
string
键查找
Dictionary

例如:

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

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

// etc
Dictionary newmorse=newdictionary();
新增(“”,“空格”);
newmorse.Add(“,”,“--…”);
加上(“.”、“-”);
加上(“?”,“.-”);
//等

你的
newmorse
字典应该是
字典
。然后,对于每个键,用单引号括起来:
newmorse.Add('A',…);加上('B',…)。您当前正试图在字典中查找键为字符串的字符。你必须有匹配的数据类型。Cory的评论和Niyoko的回答是正确的。您正在将字符串与无法工作的字符进行比较。将字符设置为字符串或将字典的键设置为字符。您的
newmorse
字典应该是
字典
。然后,对于每个键,用单引号括起来:
newmorse.Add('A',…);加上('B',…)。您当前正试图在字典中查找键为字符串的字符。你必须有匹配的数据类型。Cory的评论和Niyoko的回答是正确的。您正在将字符串与无法工作的字符进行比较。要么将字符设置为字符串,要么将字典的键设置为字符。