简体   繁体   English

如何在课堂上存储平均分和高分?

[英]How to store average score and high score in class?

I don't know how to store a player's average score, high score and average time to complete a game in my user.cs class. 我不知道如何在user.cs类中存储玩家的平均得分,高分和平均时间来完成游戏。 Whenever the player finishes a round of my game, the average and high scores and average time have to update every time in their labels. 每当玩家完成我的游戏时,平均和高分以及平均时间都必须在他们的标签中每次更新。

I've tried using arrays and arraylists but I am still unsure because neither seem to work. 我已经尝试过使用数组和数组列表,但是我仍然不确定,因为两者似乎都不起作用。

Here is my user.cs class: 这是我的user.cs类:

public class User
    {
        public string fname { get; set; } = "";
        public string lname { get; set; } = "";
        public string username { get; set; } = "";
        public string password { get; set; } = "";

        public User() { }

        public User (string fname, string lname, string username, string password)
        {
            this.fname = fname;
            this.lname = lname;
            this.username = username;
            this.password = password;
        }
    }

I also need to show the users name, username, high score, average score and timing in labels. 我还需要在标签中显示用户名,用户名,高分,平均分和时间。

The format should be double/float. 格式应为double / float。

You cannot store average score because of the way averages work. 由于平均工作方式,您无法存储平均得分。 While you can count games played by a user by simply increasing the counter by one every time a game finishes, there is no analytical form to advance the average. 虽然您可以通过在每次游戏结束时简单地将计数器增加一来对用户玩的游戏进行计数,但是没有分析形式可以提高平均值。

However, if you stored total number of games and total score, then you would be able to advance all the metrics you need. 但是,如果您存储了游戏总数和总得分,那么您将能够提高所需的所有指标。

class User
{
    public int HighScore { get; private set; } = 0;

    public double AverageScore => 
        this.GamesPlayed > 0 ? this.TotalScore / (double)this.GamesPlayed : 0;

    private int GamesPlayed { get; set; } = 0;
    private int TotalScore { get; set; } = 0;

    public void GameOver(int score)
    {
        this.HighScore = Math.Max(this.HighScore, score);
        this.GamesPlayed += 1;
        this.TotalScore += score;
    }
}

You can store the average and then recalculate after a game finishes. 您可以存储平均值,然后在游戏结束后重新计算。 This way you don't need to store a value (totalscore) that will cause overflow issues (sooner or later). 这样,您无需存储将导致溢出问题(早晚)的值(总得分)。

class User
{
    public int HighScore { get; private set; } = 0;

    public double AverageScore { get; private set; } = 0;

    private int GamesPlayed { get; set; } = 0;

    public void GameOver(int score)
    {
        this.HighScore = Math.Max(this.HighScore, score);
        // get the prev total score then increase with the current score and get the new average in the end (also increase the GamesPlayed)
        this.AverageScore = ((this.AverageScore * this.GamesPlayed) + score) / ++this.GamesPlayed;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM