用方法/数组/私人值确定最小数字

发布于 2025-01-24 13:52:39 字数 1827 浏览 0 评论 0原文

您好,我是一年级的大学生,我正在学习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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

铃予 2025-01-31 13:52:39

我不知道这是否会回答您的问题。我将对您的代码进行一些观察:

  • GetPetName不应要求该名称,只需返回即可。也许AskForname是一个更好的名字。目前,如果不首先询问用户,就无法获取名称。就个人而言,我会将数据检索放在课堂外面(业务模型)将与数据检索(基础架构)结合使用。
  • 如果GetPetName仅限于返回名称,则使用属性通常很有趣。
  • 该属性还应有能力设置其值的集合。不能查询/设置对象的内在属性是没有多大意义的。如果您无法访问其属性,该如何与宠物合作?那班级有什么用?

因此,您的班级可能是这样的:

public class Pet
{ 
    public string Name { get; set; }
    public int Age { get; set; }
    public string Breed { get; set; }
}

非常简单。在正常的发展中,课程更为复杂,并且具有对类属性做某事的方法,但是对于您的样本,这是我的个人实现。

我们可以使用一种方法向用户询问一个PET数据:

private static Pet AskUserForPet()
{
    System.Console.WriteLine("What is your dogs name?");
    var name = System.Console.ReadLine();

    int age = 0;
    var valid = false;
    while (!valid)
    {
        System.Console.WriteLine("What is your dogs age?");
        valid = int.TryParse(System.Console.ReadLine(), out age);
    }

    System.Console.WriteLine("What is your dogs breed?");
    var breed = System.Console.ReadLine();

    return new Pet
    {
        Name = name,
        Age = age,
        Breed = breed
    };
}

该方法可能在您的程序中,而不是在类中。在较大的项目中,这些事情是在基础架构层中制成的。

为了创建宠物:

System.Console.WriteLine("How many pets do you own?");
var totalPets = int.Parse(System.Console.ReadLine());

Pet[] pets = new Pet[totalPets];

for (int i = 0; i < totalPets; i++)
{
    pets[i] = AskUserForPet();

    System.Console.WriteLine(pets[i].Name + " is " + pets[i].Age + " and is a " + pets[i].Breed);
}

您可以使用以前的代码获取最年轻的宠物:

Pet youngestPet = null;
Pet[] pets = new Pet[totalPets];

for (int i = 0; i < totalPets; i++)
{
    pets[i] = AskUserForPet();

    System.Console.WriteLine(pets[i].Name + " is " + pets[i].Age + " and is a " + pets[i].Breed);

    if (youngestPet == null || pets[i].Age < youngestPet.Age)
    {
        youngestPet = pets[i];
    }
}

或者,如果您需要稍后进行:

Pet youngestPet = null;
for (int i = 0; i < pets.Length; i++)
{
    if (youngestPet == null || pets[i].Age < youngestPet.Age)
    {
        youngestPet = pets[i];
    }
}

使用LINQ,您可以简化代码:

var youngestPet = pets.Aggregate((youngest, x) => 
    youngest == null || x.Age < youngest.Age ? x : youngest);

I don't know if this will answer your question. I'm going to make some observations about your code:

  • GetPetName should not ask for the name, just return it. Maybe AskForName would be a better name. Right now it is not possible to get the name without asking the user for it first. Personally, I would put the data retrieval outside the class because depending on the case it could be possible to get it from a database, a file, a Web service, the user... and creating different methods inside the class makes the class itself (the business model) to be coupled with the data retrieval (infrastructure).
  • If GetPetName is limited to return the name, it is often interesting to use a property.
  • The property should also have a set to be able to set its value. It does not make much sense that intrinsic properties of the object cannot be queried/set. How do you work with a Pet if you cannot access its properties? What is the use of the class then?

So your class could be something like this:

public class Pet
{ 
    public string Name { get; set; }
    public int Age { get; set; }
    public string Breed { get; set; }
}

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:

private static Pet AskUserForPet()
{
    System.Console.WriteLine("What is your dogs name?");
    var name = System.Console.ReadLine();

    int age = 0;
    var valid = false;
    while (!valid)
    {
        System.Console.WriteLine("What is your dogs age?");
        valid = int.TryParse(System.Console.ReadLine(), out age);
    }

    System.Console.WriteLine("What is your dogs breed?");
    var breed = System.Console.ReadLine();

    return new Pet
    {
        Name = name,
        Age = age,
        Breed = breed
    };
}

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:

System.Console.WriteLine("How many pets do you own?");
var totalPets = int.Parse(System.Console.ReadLine());

Pet[] pets = new Pet[totalPets];

for (int i = 0; i < totalPets; i++)
{
    pets[i] = AskUserForPet();

    System.Console.WriteLine(pets[i].Name + " is " + pets[i].Age + " and is a " + pets[i].Breed);
}

You can use previous code to get the youngest pet:

Pet youngestPet = null;
Pet[] pets = new Pet[totalPets];

for (int i = 0; i < totalPets; i++)
{
    pets[i] = AskUserForPet();

    System.Console.WriteLine(pets[i].Name + " is " + pets[i].Age + " and is a " + pets[i].Breed);

    if (youngestPet == null || pets[i].Age < youngestPet.Age)
    {
        youngestPet = pets[i];
    }
}

Or, if you need do it later:

Pet youngestPet = null;
for (int i = 0; i < pets.Length; i++)
{
    if (youngestPet == null || pets[i].Age < youngestPet.Age)
    {
        youngestPet = pets[i];
    }
}

Using LINQ you can simplify the code:

var youngestPet = pets.Aggregate((youngest, x) => 
    youngest == null || x.Age < youngest.Age ? x : youngest);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文