C#动物园项目:无法将用户输入的物种与年龄存入类字段
动物园管理程序数据存储问题
正在为学校项目开发一款动物园管理程序,可记录三个独立笼子中的3种动物(狮子Lion、熊Bear、狼Wolf),但目前无法将用户输入的数据存入各动物类对应字段。
要求创建3个实现IAnimal接口的类(Lion、Bear、Wolf),该接口包含2个属性(Species、Age)和2个方法(RequestUniqueCharacteristic、GetDescription)。
核心问题:程序能获取并存储动物的独特特征,但无法存储用户输入的物种(Species)和年龄(Age),调用GetDescription()方法时仅能输出独特特征。例如输出为:"contains a 0-year-old with a red mane",其中“old”和“with”之间的空白处应为物种名称。
现有代码
IAnimal接口
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Unity; namespace TheZoo { public interface IAnimal { public string Species { get; set; } public int Age { get; set; } public void RequestUniqueCharacteristic(); public void GetDescription(); } }
Lion类
using Azure; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace TheZoo { internal class Lion:IAnimal { public string Species { get; set; } public int Age { get; set; } public string ManeColour { get; set; } public Lion() { } public Lion(int age, string species, string maneColour) { Species = species; Age = age; ManeColour = maneColour; } public void RequestUniqueCharacteristic() { Console.WriteLine("What colour is the Lions mane?"); ManeColour = Console.ReadLine(); } public void GetDescription() { Console.WriteLine($"contains a {Age}-year-old {Species} with a {ManeColour} mane."); } } }
主程序代码
using Azure; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using TheZoo; namespace TheZoo { internal class Program { static void Main(string[] args) { //variable initialization string cage; string species; int age; //Created objects within a list List<IAnimal> list = new(); Lion lion = new(); Bear bear = new(); Wolf wolf = new(); list.Add(lion); list.Add(bear); list.Add(wolf); // Start of program, prompts user to input animal attributes for (int i = 1; i <= 3; i++) { cage = "Cage " + i; Console.WriteLine(cage); Console.WriteLine("What is the animals species? (Please capitalize the first letter)"); species = Console.ReadLine(); if (species == "Lion" || species == "Bear" || species == "Wolf") { Console.WriteLine("How old is the animal?"); age = Convert.ToInt16(Console.ReadLine()); } else { Console.WriteLine("Please enter Lion, Bear, or Wolf with the first letter capitalized."); Console.WriteLine("What is the animals species? (Please capitalize the first letter)"); species = Console.ReadLine(); Console.WriteLine("How old is the animal?"); age = Convert.ToInt32(Console.ReadLine()); } if (species == "Lion") { lion.RequestUniqueCharacteristic(); Console.WriteLine(); } else if (species == "Bear") { bear.RequestUniqueCharacteristic(); Console.WriteLine(); } else if (species == "Wolf") { wolf.RequestUniqueCharacteristic(); Console.WriteLine(); } else { Console.WriteLine("Something went wrong. Please try again."); } } Console.WriteLine("-----------------------------------"); // Prints a summary from each class (Bear, Lion, Wolf) foreach (IAnimal animal in list) { animal.GetDescription(); } Console.ReadLine(); Console.WriteLine("Cages 1 - 3 have been updated."); } } }
问题分析
主程序中,用户输入的species和age变量仅被临时存储,未赋值给对应动物对象的Species和Age属性,导致这些属性保持C#默认值(int类型默认0,string类型默认空字符串),最终输出异常。
解决步骤
将输入值绑定到动物对象属性
在判断物种的分支中,添加属性赋值代码,以Lion为例:if (species == "Lion") { lion.Age = age; lion.Species = species; lion.RequestUniqueCharacteristic(); Console.WriteLine(); }同理给Bear、Wolf的分支添加对应的
Age和Species赋值逻辑。优化输入验证逻辑
当前else分支仅重新输入一次,无法确保输入正确性,建议用循环强制用户输入合法内容:// 循环验证物种输入 while (true) { Console.WriteLine("What is the animals species? (Please capitalize the first letter)"); species = Console.ReadLine(); if (species == "Lion" || species == "Bear" || species == "Wolf") { break; } Console.WriteLine("Invalid input! Please enter Lion, Bear, or Wolf with the first letter capitalized."); } // 循环验证年龄输入(确保是正整数) Console.WriteLine("How old is the animal?"); while (!int.TryParse(Console.ReadLine(), out age) || age <= 0) { Console.WriteLine("Invalid age! Please enter a valid positive integer."); }可选:使用构造函数初始化对象
若想简化属性赋值,可利用带参构造函数直接创建初始化完成的对象,调整主程序逻辑如下(以Lion为例):// 先获取独特特征 Console.WriteLine("What colour is the Lions mane?"); string maneColour = Console.ReadLine(); // 用构造函数创建已初始化的对象 Lion lion = new Lion(age, species, maneColour); // 更新列表中的对象 list[0] = lion;
内容的提问来源于stack exchange,提问作者TheStudentMans
相关产品推荐
相关产品推荐

