【问题标题】:adding user input to different sub classes according to user input根据用户输入将用户输入添加到不同的子类
【发布时间】:2023-02-01 18:08:18
【问题描述】:

(编辑:欢迎来见鬼 NYP 学生)

我刚开始学习 C#,这是我学习的第 5 周

用户将输入口袋妖怪名称 hp 和 exp 以将口袋妖怪添加到特定子类

例如 name = charmander,将发送到子类 Charmander 并具有能力 =“太阳能”

我如何能

1.) 检查并确认宠物小精灵已被发送到正确的子类并且

2.) 将输入的神奇宝贝发送到正确的子类后,使用 for 循环在列表中显示神奇宝贝,如下所示:

如果列表中有 3 个神奇宝贝,则按升序排列

==============
pokemon name: charmander
pokemon hp: 20
pokemon exp: 50
==============

==============
pokemon name: pikachu
pokemon hp: 40
pokemon exp: 10
==============

==============
pokemon name: eevee
pokemon hp: 50
pokemon exp: 90
==============

程序代码:

                Console.Write("enter pokemon name : ");
                string name = Console.ReadLine();

                
                //enters pokemon hp
                Console.Write("enter pokemon HP : ");
                int hp = Convert.ToInt32(Console.ReadLine());

                //enters pokemon EXP 
                Console.Write("enter pokemon EXP : ");
                int exp = Convert.ToInt32(Console.ReadLine());

                //to make sure ability exists in current context
                string ability = "";

                

                //enter name Validation. toupper() changes name to lowercase
                if (name.ToLower() != "charmander" && name.ToLower() != "eevee" && name.ToLower() != "pikachu") {
                    Console.WriteLine("only can add Charmander, Eevee and Pikachu!!");
                }

                //enter hp Validation
                else if (hp <= 0) {
                    Console.WriteLine("HP cannot be below 0!!");
                }

                //enter EXP validation
                else if (exp <= 0) {
                    Console.WriteLine("EXP cannot be below 0!!");
                }

                //after validating name hp and exp, will add the pokemons Name, Hp and EXP to the dictionary
                else {
                            
                    pokemonlist.Add(name.ToString()); //pokemon name

                        if (name.ToLower() == "pikachu") {
                            new Pikachu(name, hp, exp, ability);
                        }

                        if (name.ToLower() == "charmander") {
                            new Charmander(name, hp, exp, ability);
                        }
                        
                        if (name.ToLower() == "eevee") {
                            new Eevee(name, hp, exp, ability);
                        }


                    pokemonlist.Add(hp.ToString()); //pokemon hp, converts to string :v
                    pokemonlist.Add(exp.ToString()); //pokemon exp
                    Console.WriteLine("+++++++++++++++++++++++");
                    Console.WriteLine("Pokemon has been added!");
                    Console.WriteLine("+++++++++++++++++++++++");
                }

类代码:


public class Pokemon{

        public string name {get; set;}
        public string hp {get; set;}
        public string exp {get; set;}
        public string ability {get; set;}
        public string evolveTo {get; set;}
        public Pokemon(string name, int hp, int exp, string ability) {
            
        }
    }

    //child : Parent
    //individual subclasses
    public class Charmander : Pokemon {

        public Charmander(string name, int hp, int exp, string ability):base(name, hp, exp, ability) {
            this.name = "Charmander";
            this.ability = "Solar Power";
            this.evolveTo = "Charmelion";
        
        }
    }
    public class Pikachu : Pokemon {
        public Pikachu(string name, int hp, int exp, string ability):base(name, hp, exp, ability) {
            this.name = "Pikachu";
            this.ability = "Lightning Bolt";
            this.evolveTo = "Raichu";
        }
    }

    public class Eevee : Pokemon {
        public Eevee(string name, int hp, int exp, string ability):base(name, hp, exp, ability) {
            this.name = "Eevee";
            this.ability = "Run Away";
            this.evolveTo = "Flareon";
        }
    }

【问题讨论】:

    标签: c# oop


    【解决方案1】:

    pokemonlist 更改为 List&lt;Pokemon&gt;

    然后将您的代码更改为以下内容(重构标记为//===):

    Console.Write("enter pokemon name : ");
    string name = Console.ReadLine();
    
    
    //enters pokemon hp
    Console.Write("enter pokemon HP : ");
    int hp = Convert.ToInt32(Console.ReadLine());
    
    //enters pokemon EXP 
    Console.Write("enter pokemon EXP : ");
    int exp = Convert.ToInt32(Console.ReadLine());
    
    //to make sure ability exists in current context
    string ability = "";
    
    
    //=== Moved below
    //enter name Validation. toupper() changes name to lowercase
    //if (name.ToLower() != "charmander" && name.ToLower() != "eevee" && name.ToLower() != "pikachu") {
    //    Console.WriteLine("only can add Charmander, Eevee and Pikachu!!");
    //}
    
        //enter hp Validation
    if (hp <= 0)
    {
        Console.WriteLine("HP cannot be below 0!!");
    }
    
    //enter EXP validation
    else if (exp <= 0)
    {
        Console.WriteLine("EXP cannot be below 0!!");
    }
    
    //after validating name hp and exp, will add the pokemons Name, Hp and EXP to the dictionary
    else
    {
        //=== Dont need these
        //pokemonlist.Add(name.ToString()); //pokemon name
    
        bool added = false; //=== True when a pokemon has been added to the list
        if (name.ToLower() == "pikachu")
        {
            pokemonlist.Add(new Pikachu(name, hp, exp, ability)); //=== Add the new object directly to the list
            added = true;
        }
        //=== Refactored to else if blocks
        else if (name.ToLower() == "charmander")
        {
            pokemonlist.Add(new Charmander(name, hp, exp, ability));
            added = true;
        }
    
        else if (name.ToLower() == "eevee")
        {
            pokemonlist.Add(new Eevee(name, hp, exp, ability));
            added = true;
        }
        else
        {
            Console.WriteLine("only can add Charmander, Eevee and Pikachu!!");
        }
    
    
        //pokemonlist.Add(hp.ToString()); //pokemon hp, converts to string :v
        //pokemonlist.Add(exp.ToString()); //pokemon exp
    
        if (added) //=== If a pokemon has been added, print the message
        {
            Console.WriteLine("+++++++++++++++++++++++");
            Console.WriteLine("Pokemon has been added!");
            Console.WriteLine("+++++++++++++++++++++++");
        }
    }
    

    现在当你想循环添加的口袋妖怪时,只需执行以下操作:

    foreach (Pokemon poke in pokemonlist)
    {
        Console.WriteLine("==============");
        Console.WriteLine($"pokemon name: {poke.name}");
        Console.WriteLine($"pokemon hp: {poke.hp}");
        Console.WriteLine($"pokemon exp: {poke.exp}");
        Console.WriteLine("==============");
    }
    

    【讨论】:

    • “pokemonlist.Add(new Eevee(name, hp, exp, ability))”处有错误;说“无法从 PokemonPocket.Eevee”转换为“字符串”错误消息也显示其他 2 个口袋妖怪
    • 阅读第一行,将 pokemonlist 更改为键入 List&lt;Pokemon&gt; 而不是 List&lt;string&gt; 或现在的任何内容。
    猜你喜欢
    • 1970-01-01
    • 2012-07-20
    • 2020-03-31
    • 2017-12-11
    • 1970-01-01
    • 2010-12-08
    • 2016-01-31
    • 2016-07-06
    • 1970-01-01
    相关资源
    最近更新 更多