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

鲍勃

我再一次: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 =“ Nitrogen”应该打印出来:

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

我试图做一个循环,为字典中的每个加法项加1,但是它一直将原子序数输出为字典中项的总数。

为了同时打印名称和符号(“氮气”,“ N”),这里的另一个出色的助手告诉我如何获得符号,但该行:

string elementSymbol = elementDictionary[elementRequest];

仅打印出其符号,而不打印出元素名称和符号,而且我也不完全确定为什么或如何修复它。当然,通过将[elementRequest]放进去,应该同时找到“氢”和“ H”吗?还是我误会了什么?

任何帮助表示赞赏!:D

标语

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

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;
        }
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章