根据用户输入将用户输入添加到不同的子类中

  • 本文关键字:用户 子类 添加 c# oop
  • 更新时间 :
  • 英文 :


(编辑:欢迎查看NYP学生)

我刚开始学习c#,这是我学习c#的第五周了

用户将输入宝可梦名称HP和经验,以便将宝可梦添加到特定子类

eg name = charmander,将发送给子类charmander并具有能力= &;太阳能发电&;

怎么做

1.)检查并确认口袋妖怪已被发送到正确的子类和

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

如果列表中有3个口袋妖怪,按HP升序排序

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

pokemonlist改为List<Pokemon>

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

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("==============");
}

相关内容

最新更新