繁体   English   中英

从字典中删除口袋妖怪

[英]Removing Pokemon from dictionary

我在这里有这段代码,当我输入 1 时,用户可以输入口袋妖怪。

using System;
    using System.Collections.Generic;
    using System.Linq;
    
    namespace PokemonPocket
    {
        class Program
        {
            static void Main(string[] args)
            {
                // Use "Environment.Exit(0);" if you want to implement an exit of the console program
                PokemonMenu(pokemonMasters, pokemonSkills);
            }
            static void PokemonMenu(List<PokemonMaster> pokemans, List<PokemonSkill> pokeskills)
            {
                // From now on myDictionary is available for any menu option 
                var myDictionary = new Dictionary<string, string>();
    
                while (true)
                { // <- loop until exit option (key 'Q') is pressed
                    Console.WriteLine("Welcome to Pokemon Pocket App!");
                    Console.WriteLine("(1). Add Pokemon to my pocket");
                    Console.WriteLine("(2). List Pokemon(s) in my pocket");
                    Console.WriteLine("(3). Check if I can evolve Pokemon");
                    Console.WriteLine("(4). Evolve Pokemon\n");
                    Console.Write("Please only enter [1,2,3,4] or Q to exit:");
    
                    char menu = Convert.ToChar(Console.ReadLine());
    
                    if (menu == '1')
                    { //Part 1 - Adding the Pokemon
                        
                        Console.Write("Enter Pokemon Name :");
                        string name = Console.ReadLine();

                        Console.Write("Enter Pokemon HP : ");
                        int hp = Convert.ToInt32(Console.ReadLine());
    
                        Console.Write("Enter Pokemon EXP : ");
                        int exp = Convert.ToInt32(Console.ReadLine());

                        if (name != "Charmander" && name != "Eevee" && name != "Pikachu")
                        // Check that only these 3 can be added
                        {
                            Console.WriteLine("Only can add Charmander, Eevee and Pikachu!");
                        }
                        else{ //if Pokemon name is correct ->
                            if (myDictionary.Count <= 0)
                            {
                            
                                myDictionary.Add("Pokemon's Name", name);
                                myDictionary.Add("Pokemon's HP", hp.ToString());
                                myDictionary.Add("Pokemon's EXP", exp.ToString());
                                Console.WriteLine("Pokemon has been added!");
    
                            }
                    }
                }
                    else if (menu == '2'){} //need help here
                                          

当我输入 2 时,我希望删除用户输入的口袋妖怪。 我在 Python 中了解我可以使用 pop() 删除字典中的项目,但我不确定如何在 C# 中执行此操作

我认为您最好制作一个包含namehpexp的 class Pokemon ,然后将其添加到字典中。 如果您分别添加所有这些参数,您将得到三个不同的记录。 如果我查看您的代码,我认为这不是您想要做的。

要删除口袋妖怪,您只需调用字典的Remove方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM