C# 从一个文本文件中读取并搜索,看它是否是一头奶牛,以及每月的最高成本,然后将其打印到屏幕上
嘿,我在做BIT(信息技术学士),我读了一个有20行的文本文件,它被“,“我想做的是获取该文本文件,我想在“切换”菜单中找到牛奶最多的奶牛我已经按ID号进行了搜索,但我就是无法从该文本文件中找到牛奶最多的奶牛C# 从一个文本文件中读取并搜索,看它是否是一头奶牛,以及每月的最高成本,然后将其打印到屏幕上,c#,C#,嘿,我在做BIT(信息技术学士),我读了一个有20行的文本文件,它被“,“我想做的是获取该文本文件,我想在“切换”菜单中找到牛奶最多的奶牛我已经按ID号进行了搜索,但我就是无法从该文本文件中找到牛奶最多的奶牛 class Program { static void Main(string[] args) { Livestock[] animals = new Livestock[20]; int counter = 0; strin
class Program
{
static void Main(string[] args)
{
Livestock[] animals = new Livestock[20];
int counter = 0;
string myLine;
string[] words;
TextReader tr = new StreamReader("S:/BIT694/livestock.txt");
while ((myLine = tr.ReadLine()) != null)
{
words = myLine.Split(',');
int ID = int.Parse(words[0]);
string LivestockType = words[1];
int YearBorn = int.Parse(words[2]);
double CostPerMonth = double.Parse(words[3]);
double CostOfVaccination = double.Parse(words[4]);
double AmountMilk = double.Parse(words[5]);
if (LivestockType == "Cow")
{
Cow newCow = new Cow(ID, "Cow", YearBorn, CostPerMonth, CostOfVaccination, AmountMilk);
animals[counter] = newCow;
}
else if (LivestockType == "Goat")
{
Goat newGoat = new Goat(ID, "Goat", YearBorn, CostPerMonth, CostOfVaccination, AmountMilk);
animals[counter] = newGoat;
}
counter++;
}
int choice;
for (;;)
{
do
{
Console.WriteLine("--------Menu--------"); // The menue Title
Console.WriteLine();
Console.WriteLine("1) Display livestock information by ID"); // Display the livestock by ID number
Console.WriteLine("2) Display cow that produced the most milk"); // Displays the cow that porduces the most milk
Console.WriteLine("3) Display goat that produced the least amount of milk"); // Displays the goat that produces the least amount of milk
Console.WriteLine("4) Calculate farm profit"); // Calculates the farm profit
Console.WriteLine("5) Display unprofitable livestock"); // Displays the unprofitable livestock
Console.WriteLine("0) Exit"); // Exits the program
Console.WriteLine();
Console.Write("Enter an option: ");
choice = int.Parse(Console.ReadLine());
} while (choice < 0 || choice > 5);
if (choice == 0) break;
Console.WriteLine("\n");
switch(choice)
{
case 1:
Console.Write("Enter livestock ID: ");
int input = int.Parse(Console.ReadLine());
// Find animal by id
Livestock livestock = null;
for(int i = 0; i < 20; i++)
{
if(animals[i].iD == input)
{
livestock = animals[i]; // Get the animal
}
}
if(livestock != null)
{
livestock.displayInfo();
} else
{
Console.WriteLine("ID not found"); // Invaild ID
}
break;
case 2:
Console.WriteLine("Cow that produced the most Milk:");
break;
case 3:
Console.WriteLine("Goat that produced the least amount of milk:");
break;
case 4:
Console.WriteLine("Calculateion of farm profit:");
break;
case 5:
Console.WriteLine("Livestock that are not profitable:");
break;
case 0:
Environment.Exit(0);
break;
default:
Console.WriteLine("The Option that you have entered is invalid please try again");
break;
}
Console.ReadLine();
}
}
}
public class Livestock
{
private int ID; //ID Number of livestock
private string LivestockType; //Value is either "Cow" or "Goat"
private int YearBorn; //Year of birth with format YYYY (i.e. 2014)
private double CostPerMonth; //The cost per month
private double CostOfVaccination; //Annual vaccination cost
private double AmountMilk; //The amount of milk produced per day in liters
public int iD { get { return ID; } }
public string livestockType { get { return LivestockType; } }
public double costPerMonth { get { return CostPerMonth; } }
public Livestock(int ID, string LivestockType,int YearBorn,double CostPerMonth,double CostOfVaccination,double AmountMilk)
{
this.ID = ID;
this.LivestockType = LivestockType;
this.YearBorn = YearBorn;
this.CostPerMonth = CostPerMonth;
this.CostOfVaccination = CostOfVaccination;
this.AmountMilk = AmountMilk;
}
public void displayInfo()
{
Console.WriteLine();
Console.WriteLine(LivestockType);
Console.WriteLine("ID:\t\t\t {0}",iD);
Console.WriteLine("Year Born:\t\t {0}",YearBorn);
Console.WriteLine("Cost Per Month\t\t ${0}",CostPerMonth);
Console.WriteLine("Cost Of Vaccination:\t ${0}",CostOfVaccination);
Console.WriteLine("Milk Per Day:\t\t {0}liters",AmountMilk);
return;
}
}
class Cow : Livestock
{
public Cow(int ID, string LivestockType, int YearBorn, double CostPerMonth, double CostOfVaccination, double AmountMilk) : base(ID, LivestockType, YearBorn, CostPerMonth, CostOfVaccination, AmountMilk)
{
}
}
类程序
{
静态void Main(字符串[]参数)
{
牲畜[]动物=新牲畜[20];
int计数器=0;
线状糜棱线;
字符串[]个单词;
TextReader tr=新的StreamReader(“S:/BIT694/alvest.txt”);
而((myLine=tr.ReadLine())!=null)
{
words=myLine.Split(',');
int ID=int.Parse(字[0]);
字符串LivestockType=words[1];
int YearBorn=int.Parse(单词[2]);
double CostPerMonth=double.Parse(单词[3]);
double costof=double.Parse(单词[4]);
double AmountMilk=double.Parse(单词[5]);
如果(LivestockType==“Cow”)
{
Cow newCow=新奶牛(ID,“Cow”,一岁出生,每月成本,接种成本,牛奶数量);
动物[柜台]=新牛;
}
else if(LivestockType==“Goat”)
{
山羊-新山羊=新山羊(ID,“山羊”,一岁出生,每月成本,接种成本,牛奶数量);
动物[柜台]=新山羊;
}
计数器++;
}
智力选择;
对于(;;)
{
做
{
Console.WriteLine(“--Menu------”;//菜单标题
Console.WriteLine();
Console.WriteLine(“1)按ID显示牲畜信息”);//按ID号显示牲畜
Console.WriteLine(“2)显示产奶最多的奶牛”);//显示产奶最多的奶牛
Console.WriteLine(“3)显示产奶量最少的山羊”);//显示产奶量最少的山羊
Console.WriteLine(“4)计算农场利润”);//计算农场利润
Console.WriteLine(“5)显示无利可图的牲畜”);//显示无利可图的牲畜
Console.WriteLine(“0)Exit”);//退出程序
Console.WriteLine();
编写(“输入选项:”);
choice=int.Parse(Console.ReadLine());
}而(选择<0 | |选择>5);
如果(选项==0)中断;
Console.WriteLine(“\n”);
开关(选择)
{
案例1:
控制台。写入(“输入牲畜ID:”;
int input=int.Parse(Console.ReadLine());
//通过id查找动物
家畜=空;
对于(int i=0;i<20;i++)
{
if(动物[i].iD==输入)
{
家畜=动物[我];//得到动物
}
}
if(牲畜!=null)
{
displayInfo();
}否则
{
Console.WriteLine(“找不到ID”);//Invaild ID
}
打破
案例2:
Console.WriteLine(“产奶最多的奶牛:”);
打破
案例3:
控制台.WriteLine(“产奶量最少的山羊:”);
打破
案例4:
Console.WriteLine(“农场利润计算:”);
打破
案例5:
Console.WriteLine(“不盈利的牲畜:”);
打破
案例0:
环境。退出(0);
打破
违约:
Console.WriteLine(“您输入的选项无效,请重试”);
打破
}
Console.ReadLine();
}
}
}
公营牲畜
{
private int ID;//牲畜的ID号
私有字符串LivestockType;//值为“Cow”或“Goat”
private int YearBorn;//格式为YYYY的出生年份(即2014年)
private double CostPerMonth;//每月的成本
私人双重接种成本;//年度接种成本
私人双倍牛奶;//每天生产的牛奶量(升)
公共int iD{get{return iD;}}
公共字符串livestockType{get{return livestockType;}}
公共双costPerMonth{get{return costPerMonth;}}
公共牲畜(整数ID,字符串牲畜类型,整数年出生,双倍成本每月,双倍接种成本,双倍牛奶)
{
this.ID=ID;
this.LivestockType=LivestockType;
this.YearBorn=YearBorn;
this.CostPerMonth=CostPerMonth;
this.costof疫苗接种=疫苗接种成本;
this.AmountMilk=AmountMilk;
}
public void displayInfo()
{
Console.WriteLine();
Console.WriteLine(LivestockType);
WriteLine(“ID:\t\t\t{0}”,ID);
WriteLine(“出生年份:\t\t{0}”,出生年份);
WriteLine(“每月成本\t\t${0}”,每月成本);
WriteLine(“疫苗接种成本:\t${0}”,疫苗接种成本);
WriteLine(“每天牛奶:\t\t{0}升”,AmountMilk);
返回;
}
}
牛类:家畜
{
公共奶牛(国际标识,字符串牲畜类型,国际年出生,双成本每月,双成本接种,双倍牛奶):基础(国际标识,牲畜类型,年出生,成本每月,Co
case 2:
Cow cowWithBestMilk; //Used to save the cow with the most milk production
///Lets Search
for( int i = 0; i < animals.Count; ++i)
{
//check lifestock, but only cows
if( LivestockType == "Cow" )
{
//Amount to check of my livestock
double livestockMilk = animals[i].getMilkAmount(); //<-- fantasy code, you have to acces here the AmountMilk of your cow
if(cowWithBestMilk != null)
{
//check if the cow from the lifestock got more milk than the other cow
if( livestockMilk > cowWithBestMilk.getMilkAmount() ) //<-- you can use >= instead of > if you want to have the last cow with the same amount
{
cowWithBestMilk = animals[i];
}
}
else
{
//there was no other cow until now
cowWithBestMilk = animals[i];
}
} //if type cow
} //for
if( cowWithBestMilk != null)
{
Console.WriteLine("Cow that produced the most Milk:" + cowWithBestMilk.getHerIdAsString() ); //<--fantasy code here
}
else
{
Console.WriteLine("Who let the cows out?");
}
break;
public string SomeProperty { get; set; }
Livestock maxCow = animals.Where(animal => animal.LivestockType == "Cow").OrderByDescending(animal => animal.AmountMilk).FirstOrDefault();
animals.Where(animal => animal.LivestockType == "Cow"
static void Main(string[] args)
{
Livestock[] animals = new Livestock[20];
int counter = 0;
string myLine;
string[] words;
TextReader tr = new StreamReader("S:/BIT694/livestock.txt");
while ((myLine = tr.ReadLine()) != null)
{
words = myLine.Split(',');
int ID = int.Parse(words[0]);
string LivestockType = words[1];
int YearBorn = int.Parse(words[2]);
double CostPerMonth = double.Parse(words[3]);
double CostOfVaccination = double.Parse(words[4]);
double AmountMilk = double.Parse(words[5]);
if (LivestockType == "Cow")
{
Cow newCow = new Cow(ID, "Cow", YearBorn, CostPerMonth, CostOfVaccination, AmountMilk);
animals[counter] = newCow;
}
else if (LivestockType == "Goat")
{
Goat newGoat = new Goat(ID, "Goat", YearBorn, CostPerMonth, CostOfVaccination, AmountMilk);
animals[counter] = newGoat;
}
counter++;
}
int choice;
for (;;)
{
do
{
Console.WriteLine("--------Menu--------"); // The menue Title
Console.WriteLine();
Console.WriteLine("1) Display livestock information by ID"); // Display the livestock by ID number
Console.WriteLine("2) Display cow that produced the most milk"); // Displays the cow that porduces the most milk
Console.WriteLine("3) Display goat that produced the least amount of milk"); // Displays the goat that produces the least amount of milk
Console.WriteLine("4) Calculate farm profit"); // Calculates the farm profit
Console.WriteLine("5) Display unprofitable livestock"); // Displays the unprofitable livestock
Console.WriteLine("0) Exit"); // Exits the program
Console.WriteLine();
Console.Write("Enter an option: ");
choice = int.Parse(Console.ReadLine());
} while (choice < 0 || choice > 5);
if (choice == 0) break;
Console.WriteLine("\n");
switch (choice)
{
case 1:
Console.Write("Enter livestock ID: ");
int input = int.Parse(Console.ReadLine());
Livestock livestock = animals.Where(animal => animal.ID == input).ToList().FirstOrDefault();
if (livestock != null)
{
livestock.displayInfo();
}
else
{
Console.WriteLine("ID not found"); // Invaild ID
}
break;
case 2:
Console.WriteLine("Cow that produced the most Milk:");
Livestock maxCow = animals.Where(animal => animal.LivestockType == "Cow").OrderByDescending(animal => animal.AmountMilk).FirstOrDefault();
if (maxCow != null)
maxCow.displayInfo();
else
Console.WriteLine("No cow");
break;
case 3:
Console.WriteLine("Goat that produced the least amount of milk:");
break;
case 4:
Console.WriteLine("Calculateion of farm profit:");
break;
case 5:
Console.WriteLine("Livestock that are not profitable:");
break;
case 0:
Environment.Exit(0);
break;
default:
Console.WriteLine("The Option that you have entered is invalid please try again");
break;
}
Console.ReadLine();
}
}
}
public class Livestock
{
public int ID { get; set; } //ID Number of livestock
public string LivestockType {get; set;} //Value is either "Cow" or "Goat"
public int YearBorn { get; set; } //Year of birth with format YYYY (i.e. 2014)
public double CostPerMonth { get; set; }//The cost per month
public double CostOfVaccination { get; set; } //Annual vaccination cost
public double AmountMilk { get; set; } //The amount of milk produced per day in liters
public Livestock(int ID, string LivestockType, int YearBorn, double CostPerMonth, double CostOfVaccination, double AmountMilk)
{
this.ID = ID;
this.LivestockType = LivestockType;
this.YearBorn = YearBorn;
this.CostPerMonth = CostPerMonth;
this.CostOfVaccination = CostOfVaccination;
this.AmountMilk = AmountMilk;
}
public void displayInfo()
{
Console.WriteLine();
Console.WriteLine(LivestockType);
Console.WriteLine("ID:\t\t\t {0}", iD);
Console.WriteLine("Year Born:\t\t {0}", YearBorn);
Console.WriteLine("Cost Per Month\t\t ${0}", CostPerMonth);
Console.WriteLine("Cost Of Vaccination:\t ${0}", CostOfVaccination);
Console.WriteLine("Milk Per Day:\t\t {0}liters", AmountMilk);
return;
}
}