用方法/数组/私人值确定最小数字
您好,我是一年级的大学生,我正在学习C#
我的问题是关于方法并确定所需值私有的最小X数字,
因此更确切地说是我正在处理代码,并且用户可以输入尽可能多的宠物正如他们想要的那样,我将收集信息名称/年龄/品种/,但是我在确定最小/最小的年龄方面遇到了麻烦。由于我无法直接分配宠物[0] .age>宠物[i]。
很抱歉,如果这是一个措辞很差的问题,但我希望我对您的理解和回答足以解释。
代码
int totalPets;
System.Console.WriteLine("How many pets do you own?");
totalPets = int.Parse(System.Console.ReadLine()
Pet[] pets;
pets = new Pet[totalPets];
int small;
for (int i = 0; i < totalPets; i++)
{
pets[i] = new Pet();
pets[i].GetPetName();
pets[i].GetPetAge();
pets[i].GetPetBreed();
pets[i].PrintPetInfo();
}
pets[0].AssignSmall();
for(int i = 0; i < totalPets; i++)
{
if(pets[0].AssignSmall < )
}
类代码
internal class Pet
private string Name;
public string GetPetName()
{
System.Console.WriteLine("What is your dogs name?");
Name = System.Console.ReadLine();
return Name;
}
private int Age;
public int GetPetAge()
{
System.Console.WriteLine("What is your dogs age?");
return this.Age = int.Parse(System.Console.ReadLine());
}
private string Breed;
public string GetPetBreed()
{
System.Console.WriteLine("What is your dogs breed?");
return this.Breed = System.Console.ReadLine();
}
public void PrintPetInfo()
{
System.Console.WriteLine(this.Name + " is " + this.Age + " and is a " + Breed);
}
public int AssignSmall()
{
int small;
small = this.Age;
return small;
}
}
}
}
Hello I am a first year college student and I am learning C#
My question is regarding methods and determining the smallest number of x numbers when the values required are private
So to be more exact I am working on code and the user can enter as many pets as they want/have and I will gather the information Name/Age/Breed/ But I am having trouble with determining the youngest/smallest age. Since I cannot directly assign pets[0].Age > pets[i].Age of the class since it is private.
Apologies if this is a poorly phrased question but I hope I explained it well enough for you to understand and answer.
CODE
int totalPets;
System.Console.WriteLine("How many pets do you own?");
totalPets = int.Parse(System.Console.ReadLine()
Pet[] pets;
pets = new Pet[totalPets];
int small;
for (int i = 0; i < totalPets; i++)
{
pets[i] = new Pet();
pets[i].GetPetName();
pets[i].GetPetAge();
pets[i].GetPetBreed();
pets[i].PrintPetInfo();
}
pets[0].AssignSmall();
for(int i = 0; i < totalPets; i++)
{
if(pets[0].AssignSmall < )
}
CLASS CODE
internal class Pet
private string Name;
public string GetPetName()
{
System.Console.WriteLine("What is your dogs name?");
Name = System.Console.ReadLine();
return Name;
}
private int Age;
public int GetPetAge()
{
System.Console.WriteLine("What is your dogs age?");
return this.Age = int.Parse(System.Console.ReadLine());
}
private string Breed;
public string GetPetBreed()
{
System.Console.WriteLine("What is your dogs breed?");
return this.Breed = System.Console.ReadLine();
}
public void PrintPetInfo()
{
System.Console.WriteLine(this.Name + " is " + this.Age + " and is a " + Breed);
}
public int AssignSmall()
{
int small;
small = this.Age;
return small;
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不知道这是否会回答您的问题。我将对您的代码进行一些观察:
因此,您的班级可能是这样的:
非常简单。在正常的发展中,课程更为复杂,并且具有对类属性做某事的方法,但是对于您的样本,这是我的个人实现。
我们可以使用一种方法向用户询问一个PET数据:
该方法可能在您的程序中,而不是在类中。在较大的项目中,这些事情是在基础架构层中制成的。
为了创建宠物:
您可以使用以前的代码获取最年轻的宠物:
或者,如果您需要稍后进行:
使用LINQ,您可以简化代码:
I don't know if this will answer your question. I'm going to make some observations about your code:
So your class could be something like this:
Very simple. In normal developments, classes are more complex and have methods to do something with the class properties, but with your sample, this is my personal implementation.
We can use a method to ask user for one Pet data:
That method maybe in your Program.cs, not in the class. In bigger projects, that kind of things are made in the Infrastructure layer.
And for the creation of your pets:
You can use previous code to get the youngest pet:
Or, if you need do it later:
Using LINQ you can simplify the code: