我如何用数组创建一个类 [英] How I can create a class with array

查看:77
本文介绍了我如何用数组创建一个类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对这个问题有疑问

用C#编写程序,然后假设你有一个数组(4,6)大小

玩家。 (身份证,姓名,身高,体重)。

身份证,姓名,身高,体重



101 102 103 104 105 106
UVWXYZ

155cm 180cm 174cm 168cm 165cm 177cm

75kg 70kg 65kg 68kg 80kg 63kg



找到以下内容:

1.打印ID和他们之间最高的玩家姓名。

2.打印ID和获得最高权重的玩家姓名。

3.每位体重低于70克的球员的打印ID和名称。

4.找出所有球员身高和体重的平均值。



任何人都可以解决这个问题吗?



我尝试了什么:



Hi, i have a problem with this question
Write a program in C# using classes then suppose you have an array in (4,6) size of
Players. (ID, Name, Tallness, Weight).
ID, Name, Tallness, Weight

101 102 103 104 105 106
U V W X Y Z
155cm 180cm 174cm 168cm 165cm 177cm
75kg 70kg 65kg 68kg 80kg 63kg

Find the following:
1. Print ID and Name of player who is Tallest between them.
2. Print ID and Name of player who get highest weight.
3. Print ID and Name of each player who have a weight of less than 70g.
4. Find the average of all players tallness and weights.

anyone can solve this?

What I have tried:

class player
        {
            string[,] ply = new string[4, 6]
                { {"101","102","103","104","105","106" },
                {"u","v","w","q","a","s"},
                {"155cm","145cm","150cm","156cm","140cm","130cm"},
                {"50kg","20kg","70kg","54kg","66kg","90kg"} };
}

推荐答案

尽管你上面的评论,这是一个明显的功课问题。但由于这个问题没有多大意义,我会给你一个建议。



首先,学习如何用C#创建类:类(C#编程指南)| Microsoft Docs [ ^ ]。



其次问你的老师语句的含义是什么然后假设你有一个数组(4) ,6)尺寸。它是指这个类的4个对象,还是4组6个对象?
Well despite your comment above this is an obvious homework question. But since the question does not make much sense I will give you a suggestion.

Firstly, learn how to create classes in C#: Classes (C# Programming Guide) | Microsoft Docs[^].

Secondly ask your teacher what is meant by the statement "then suppose you have an array in (4,6) size". Does it mean 4 objects of this class, or 4 groups of 6 objects?


这里,把它交给它。它创建了一个4x6的玩家阵列...



(我猜想这是你得到的唯一有代码的解决方案。)



Here, hand this in. It creates a 4x6 array of players...

(I'm gonna venture a guess that this is the only solution you get that has code in it.)

public class Player
{
    public int ID { get; set; }
    public string Name { get; set; }
    public int Height { get; set; }
    public int Weight { get; set; }
}

public class Players
{
    public double AvgWeight
    {
        get
        {
            List<Player> flat = this.Flatten(this.ThePlayers).ToList();
            return Math.Round(flat.Average(x=>x.Weight), 1);
        }
    }
    public double AvgHeight
    {
        get
        {
            List<Player> flat = this.Flatten(this.ThePlayers).ToList();
            return Math.Round(flat.Average(x=>x.Height),1);
        }
    }
    public string Tallest
    {
        get
        {
            List<Player> flat = this.Flatten(this.ThePlayers).ToList();
            Player player = flat.FirstOrDefault(x=>x.Height == flat.Max(y=>y.Height));
            return string.Format("Tallest player: {0} {1}", player.ID, player.Name);
        }
    }

    public string Heaviest
    {
        get
        {
            List<Player> flat = this.Flatten(this.ThePlayers).ToList();
            Player player = flat.FirstOrDefault(x=>x.Weight == flat.Max(y=>y.Weight));
            return string.Format("Tallest player: {0} {1}", player.ID, player.Name);
        }
    }

    public int RandomWeight()
    {
        return random.Next(63,80);
    }

    public int RandomHeight()
    {
        return random.Next(155,180);
    }

    public IEnumerable<T> Flatten<T>(T[,] map) 
    {
        for (int row = 0; row < map.GetLength(0); row++) 
        {
            for (int col = 0; col < map.GetLength(1); col++) 
            {
                yield return map[row,col];
            }
        }
    }

    private Player[,] ThePlayers = null;

    Random random = new Random();

    public Players()
    {
        this.ThePlayers = new Player[4,6]
        {
            { 
                new Player(){ID=101, Height=RandomHeight(), Name="Adam", Weight=random.Next(63,80)},
                new Player(){ID=102, Height=RandomHeight(), Name="Zach", Weight=random.Next(63,80)},
                new Player(){ID=103, Height=RandomHeight(), Name="Andy", Weight=random.Next(63,80)},
                new Player(){ID=104, Height=RandomHeight(), Name="Bob", Weight=random.Next(63,80)},
                new Player(){ID=105, Height=RandomHeight(), Name="Sam", Weight=random.Next(63,80)},
                new Player(){ID=106, Height=RandomHeight(), Name="John", Weight=random.Next(63,80)}
            },
            { 
                new Player(){ID=107, Height=random.Next(155,180), Name="Pradeep", Weight=random.Next(63,80)},
                new Player(){ID=108, Height=random.Next(155,180), Name="Panda", Weight=random.Next(63,80)},
                new Player(){ID=109, Height=random.Next(155,180), Name="Deepak", Weight=random.Next(63,80)},
                new Player(){ID=110, Height=random.Next(155,180), Name="Reetard", Weight=random.Next(63,80)},
                new Player(){ID=111, Height=random.Next(155,180), Name="Dumass", Weight=random.Next(63,80)},
                new Player(){ID=112, Height=random.Next(155,180), Name="Skinflute", Weight=random.Next(63,80)}
            },
            { 
                new Player(){ID=113, Height=random.Next(155,180), Name="Analpore", Weight=random.Next(63,80)},
                new Player(){ID=114, Height=random.Next(155,180), Name="Sumyunguy", Weight=random.Next(63,80)},
                new Player(){ID=115, Height=random.Next(155,180), Name="Tittiefreak", Weight=random.Next(63,80)},
                new Player(){ID=116, Height=random.Next(155,180), Name="Carpetmuncher", Weight=random.Next(63,80)},
                new Player(){ID=117, Height=random.Next(155,180), Name="Dickwad", Weight=random.Next(63,80)},
                new Player(){ID=118, Height=random.Next(155,180), Name="Numnuts", Weight=random.Next(63,80)}
            },
            { 
                new Player(){ID=119, Height=random.Next(155,180), Name="Asspilot", Weight=random.Next(63,80)},
                new Player(){ID=120, Height=random.Next(155,180), Name="Budonkidonks", Weight=random.Next(63,80)},
                new Player(){ID=121, Height=random.Next(155,180), Name="Lactator", Weight=random.Next(63,80)},
                new Player(){ID=122, Height=random.Next(155,180), Name="Loverope", Weight=random.Next(63,80)},
                new Player(){ID=123, Height=random.Next(155,180), Name="Pearlchin", Weight=random.Next(63,80)},
                new Player(){ID=124, Height=random.Next(155,180), Name="Bukake", Weight=random.Next(63,80)}
            }

        };
    }

    public List<Player> GetUnderWeight(int weight)
    {
        List<Player> players = this.Flatten(this.ThePlayers).Where(x=>x.Weight < 70).ToList();
        return players;
    }

}





用法:





Usage:

class Program
{
    static void Main(string[] args)
    {
        Players players = new Players();
        double avgHeight = players.AvgHeight;
        double avgWeight = players.AvgWeight;
        string giant = players.Tallest;
        string tubbo = players.Heaviest;
        List<Player> wimps = players.GetUnderWeight(70);
    }
}


这篇关于我如何用数组创建一个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆