C# - 根据用户输入在字典中打印一个键及其值?

时间:2016-09-30 15:47:25

标签: c# dictionary input output

我又来了:D

这是我目前的代码:

   class Program
    {
        static void Main(string[] args)
        {
            // Declare all numbers
            int electronNumber;
            int protonNumber;
            float neutronNumber;
            int i;
            int indexOfElementName;
            int restartInt;
            restartInt = 1;

            // Declare all strings
            string elementRequest;
            string restartString;

            var elementDictionary = new Dictionary<string, string>
            {
            { "Hydrogen", "H" },
            { "Helium", "He" },
            { "Lithium", "Li" },
            { "Beryllium", "Be" },
            { "Boron", "B" },
            { "Carbon", "C" },
            { "Nitrogen", "N" },
            { "Oxygen", "O" },
            { "Fluroine", "F" },
            { "Neon", "Ne" },
            { "Sodium", "Na" },
            { "Magnesium", "Mg" },
            { "Aluminium", "Al" },
            { "Silicon", "Si" },
            { "Phosphorus", "P" },
            { "Sulfur", "S" },
            { "Chlorine", "Cl" },
            { "Argon", "Ar" },
            { "Potassium", "K" },
            { "Calcium", "Ca" },
            { "Scandium", "Sc" },
            { "Titanium", "Ti" },
            { "Vanadium", "V" },
            { "Chromium", "Cr" },
            { "Manganese", "Mn" },
            { "Iron", "Fe" },
            { "Cobalt", "Co" },
            { "Nickel", "Ni" },
            { "Copper", "Cu" },
            { "Zinc", "Zn" },
            { "Gallium", "Ga" },
            { "Germanium", "Ge" },
            { "Arsenic", "As" },
            { "Selenium", "Se" },
            { "Bromine", "Br" },
            { "Krypton", "Kr" },
            { "Rubidium", "Rb" },
            { "Strontium", "Sr" },
            { "Yttrium", "Y" },
            { "Zirconium", "Zr" },
            { "Niobium", "Nb" },
            { "Molybdenum", "Mo" },
            { "Technetium", "Tc" },
            { "Ruhenium", "Ru" },
            { "Palladium", "Pd" },
            { "Silver", "Ag" },
            { "Cadmium", "Cd" },
            { "Indium", "In" },
            { "Tin", "Sn" },
            { "Antimony", "Sb" },
            { "Tellurium", "Te" },
            { "Iodine", "I" },
            { "Xenon", "Xe" },
            { "Caesium", "Cs" },
            { "Barium", "Ba" },
            { "Lanthanum", "La" },
            { "Cerium", "Ce" },
            { "Praesodynium", "Pr" },
            { "Neodynium", "Nd" },
            { "Promethium", "Pm" },
            { "Samarium", "Sm" },
            { "Europium", "Eu" },
            { "Gadolinium", "Gd" },
            { "Terbium", "Tb" },
            { "Dysprosium", "Dy" },
            { "Holmium", "Ho" },
            { "Erbium", "Er" },
            { "Thulium", "Tm" },
            { "Ytterbium", "Yb" },
            { "Lutetium", "Lu" },
            { "Hafnium", "Hf" },
            { "Tantalum", "Ta" },
            { "Tungsten", "W" },
            { "Rhenium", "Re" },
            { "Osmium", "Os" },
            { "Iridium", "Ir" },
            { "Platinum", "Pt" },
            { "Gold", "Au" },
            { "Mercury", "Hg" },
            { "Thallium", "Tl" },
            { "Lead", "Pb" },
            { "Bismuth", "Bi" },
            { "Polonium", "Po" },
            { "Astatine", "At" },
            { "Radon", "Rn" },
            { "Francium", "Fr" },
            { "Radium", "Ra" },
            { "Actinium", "Ac" },
            { "Thorium", "Th" },
            { "Protactinium", "Pa" },
            { "Uranium", "U" },
            { "Neptunium", "Np" },
            { "Plutonium", "Pu" },
            { "Americium", "Am" },
            { "Curium", "Cm" },
            { "Berkelium", "Bk" },
            { "Californium", "Cf" },
            { "Einsteinium", "Es" },
            { "Fermium", "Fm" },
            { "Mendelevium", "Md" },
            { "Nobelium", "No" },
            { "Lawrencium", "Lr" },
            { "Rutherfordium", "Rf" },
            { "Dubnium", "Db" },
            { "Seaborgium", "Sg" },
            { "Bohrium", "Bh" },
            { "Hassium", "Hs" },
            { "Meitnerium", "Mt" },
            { "Darmstadtium", "Ds" },
            { "Roentgenium", "Rg" },
            { "Copernicium", "Cn" },

            };
                while (restartInt == 1)
            {
                Console.WriteLine("What element do you want? Either input it's full name, with a capital letter. E.g 'Hydrogen'");
                elementRequest = Console.ReadLine();
                string elementSymbol = elementDictionary[elementRequest];
                Console.WriteLine("Your element: " + elementRequest + " has the element symbol of: " + elementSymbol);

                    Console.WriteLine("Would you like to try again? Type '1' for yes, and '2' for no.");
                restartString = Console.ReadLine();
                int.TryParse(restartString, out restartInt);


            };
        }
    }

由于字典列表是元素各自原子序数的顺序,我如何从开始到输入元素计数,以便我可以找到元素原子序数。另外,我怎样才能简单地打印元素名称及其符号?

例如:

elementRequest =“氮气”应打印出来:

元素名称 - “氮” 元素符号 - “N” 元素的原子序数= 7

我尝试做一个循环,为每个添加添加1到字典中,但它一直输出原子序数作为字典中的项目总数。

为了打印名称和符号,(“氮气”,“N”),这里的另一个梦幻般的帮手告诉我如何获得符号,但行:

string elementSymbol = elementDictionary[elementRequest];

只打印出它的符号,而不是它的元素名称和符号,我不完全确定为什么或如何修复它。肯定通过把[elementRequest]放入应该找到“氢”和“H”?或者我误解了什么?

任何帮助表示赞赏! :d

1 个答案:

答案 0 :(得分:2)

也许尝试一个类来表示元素。

public class Element
{
    public string Name { get; set; }
    public string Symbol { get; set;}
    public int AtomicNumber { get; set; }

    public Element(string name, string symbol, int atomicNumber)
    {
        Name = name;
        Symbol = symbol;
        AtomicNumber = atomicNumber;
    }
}

添加到这样的词典:

var elementDictionary = new Dictionary<string, Element>
{
        { "Hydrogen", new Element("Hydrogen", "H", 1) },
        { "Helium", new Element("Helium", "He", 2) },

        //etc.
};

然后当您在字典中查找时,您将获得该类,因此可以访问其所有属性

elementRequest = Console.ReadLine();
var element = elementDictionary[elementRequest];
Console.WriteLine("Your element: " + element.Name + " has the element symbol of: " + element.Symbol + " and atomic number of: " + element.AtomicNumber);

然后,您可以向元素类中添加更多属性,并在从字典中搜索时将它们全部可用

编辑 - 完成的代码测试和工作

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            int restartInt;
            restartInt = 1;

            // Declare all strings
            string elementRequest;
            string restartString;

            var elementDictionary = new Dictionary<string, Element>
            {
                { "Hydrogen", new Element("Hydrogen", "H", 1) },
                { "Helium", new Element("Helium", "He", 2) },

                //etc.
            };

            while (restartInt == 1)
            {
                Console.WriteLine("What element do you want? Either input it's full name, with a capital letter. E.g 'Hydrogen'");

                elementRequest = Console.ReadLine();
                var element = elementDictionary[elementRequest];
                Console.WriteLine("Your element: " + element.Name + " has the element symbol of: " + element.Symbol + " and atomic number of: " + element.AtomicNumber);

                Console.WriteLine("Would you like to try again? Type '1' for yes, and '2' for no.");
                restartString = Console.ReadLine();
                int.TryParse(restartString, out restartInt);
            };
        }
    }


    public class Element
    {
        public string Name { get; set; }
        public string Symbol { get; set; }
        public int AtomicNumber { get; set; }

        public Element(string name, string symbol, int atomicNumber)
        {
            Name = name;
            Symbol = symbol;
            AtomicNumber = atomicNumber;
        }
    }
}