如何输入名称并在C#中以拼音字母输出

时间:2018-02-27 14:01:09

标签: c# console-application

public static void militarySpell() 
{
    Console.WriteLine("Type");
    string spell = Convert.ToString(Console.ReadLine());
    switch (spell) 
    {
        case("a"):
            string a = "Alfa";
            spell = a;
            Console.WriteLine(spell);
            break;
        case ("b"):
            string b = "Bravo";
            spell = b;
            Console.WriteLine(spell);
            break;
        case ("c"):
            string c = "Charlie";
            spell = c;
            Console.WriteLine(spell);
            break;
        case ("d"):
            string d = "Delta";
            Console.WriteLine(d);
            break;
        case ("e"):
            string e = "Echo";
            Console.WriteLine(e);
            break;
        case ("f"):
            string f = "Foxtrot";
            Console.WriteLine(f);
            break;
        case ("g"):
            string g = "Golf";
            Console.WriteLine(g);
            break;
        case ("h"):
            string h = "Hotel";
            Console.WriteLine(h);
            break;
        case ("i"):
            string i = "India";
            Console.WriteLine(i);
            break;
        case ("j"):
            string j = "Juliet";
            Console.WriteLine(j);
            break;
        case ("k"):
            string k = "Kilo";
            Console.WriteLine(k);
            break;
        case ("l"):
            string l = "Lima";
            Console.WriteLine(l);
            break;
        case ("m"):
            string m = "Mike";
            Console.WriteLine(m);
            break;
        case ("n"):
            string n = "November";
            Console.WriteLine(n);
            break;
        case ("o"):
            string o = "Oscar";
            Console.WriteLine(o);
            break;
        case ("p"):
            string p = "Papa";
            Console.WriteLine(p);
            break;
        case ("q"):
            string q = "Quebec";
            Console.WriteLine(q);
            break;
        case ("r"):
            string r = "Romeo";
            Console.WriteLine(r);
            break;
        case ("s"):
            string s = "Sierra";
            Console.WriteLine(s);
            break;
        case ("t"):
            string t = "Tango";
            Console.WriteLine(t);
            break;
        case ("u"):
            string u = "Uniform";
            Console.WriteLine(u);
            break;
        case ("v"):
            string v = "Victor";
            Console.WriteLine(v);
            break;
        case ("w"):
            string w = "Whiskey";
            Console.WriteLine(w);
            break;
        case ("x"):
            string x = "X-Ray";
            Console.WriteLine(x);
            break;
        case ("y"):
            string y = "Yankee";
            Console.WriteLine(y);
            break;
        case ("z"):
            string z = "Zulu";
            Console.WriteLine(z);
            break;
    }   
    Console.ReadLine();
}

这是我的代码,但问题是它只输出一个我要输入名称的单词,如“Matt”和输出(Mike Alfa Tango Tango)。

3 个答案:

答案 0 :(得分:2)

Dictionary

的完美用例
Dictionary<char, string> dDict = new Dictionary<char, string>() {{'a', "Alfa"}, {'b', "Bravo"}, {'c', "Charlie"}, {'d', "Delta"}, {'e', "Echo"}, {'f', "Foxtrot"}, {'g', "Golf"}, {'h', "Hotel"}, {'i', "India"}, {'j', "Juliet"}, {'k', "Kilo"}, {'l', "Lima"}, {'m', "Mike"}, {'n', "November"}, {'o', "Oscar"}, {'p', "Papa"}, {'q', "Quebec"}, {'r', "Romeo"}, {'s', "Sierra"}, {'t', "Tango"}, {'u', "Uniform"}, {'v', "Victor"}, {'w', "Whiskey"}, {'x', "X-Ray"}, {'y', "Yankee"}, {'z', "Zulu"}};
string result = string.Join(" ", "Matt".Select(x => dDict[char.ToLower(x)]));

https://dotnetfiddle.net/VsD5Mc

答案 1 :(得分:1)

提取模型,在您的情况下,字母及其名称对应:

private static Dictionary<char, string> s_Alphabet = new Dictionary<char, string>() {
  { 'a', "Alpha"},
   ...
  { 'z', "Zulu"},
};

你可以像

一样轻松
private static string ToAlphabet(string value) {
  return string.Join(Environment.NewLine, value
    .Select(c => s_Alphabet.TryGetValue(c, out var v) 
       ? v               // we know the letter, e.g. 'a', 'e'
       : c.ToString())); // we don't know the letter, e.g. space, full stop
}

如果您希望对ADZ等大写字母进行编码,只需添加

char.ToLower(),即:

private static string ToAlphabet(string value) {
   return string.Join(Environment.NewLine, value
     .Select(c => s_Alphabet.TryGetValue(char.ToLower(c), out var v) 
        ? v               // we know the letter, e.g. 'a', 'e'
        : c.ToString())); // we don't know the letter, e.g. space, full stop
 }

答案 2 :(得分:0)

我想我看到了你的问题。您需要使用loop来构建字符串。

  Console.WriteLine("Type");
  StringBuilder sb = new StringBuilder();     
  string spell = Convert.ToString(Console.ReadLine());
  foreach (var c in spell.ToUpperInvariant().ToCharArray())
  { //make it uppercase to eliminate case sensitivity
     switch (c) 
     {
        case('A'):  //case now evaluates uppercase chars, so single quotes               
            sb.Append("Alfa "); //no need for the local var but add a space
            break;
        //your other cases omitted for brevity
     }   

  }
  Console.WriteLine(sb.ToString());
  Console.ReadLine();

修改为首先接受整个单词。请注意,在通过开关和开关

运行之前,您需要将字符转换为Upper