c# -根据用户的输入在字典中打印出键及其值
本文关键字:打印 字典 -根 用户 输入 | 更新日期: 2023-09-27 18:09:37
me again: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]在应该找到"氢"answers"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;
}
}
}