亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何存儲課堂平均分和最高分?

如何存儲課堂平均分和最高分?

C#
蕭十郎 2022-12-24 10:00:06
我不知道如何在我的 user.cs 類中存儲玩家的平均分數、高分和完成游戲的平均時間。每當玩家完成我的一輪游戲時,平均分和最高分以及平均時間都必須在他們的標簽中每次更新。我試過使用數組和數組列表,但我仍然不確定,因為它們似乎都不起作用。這是我的 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;        }    }我還需要在標簽中顯示用戶名、用戶名、高分、平均分和時間。格式應為雙精度/浮點數。
查看完整描述

2 回答

?
MYYA

TA貢獻1868條經驗 獲得超4個贊

由于平均值的工作方式,您無法存儲平均分數。雖然您可以通過每次游戲結束時將計數器簡單地增加一個來計算用戶玩過的游戲,但是沒有分析形式來提高平均值。


但是,如果您存儲了游戲總數和總得分,那么您將能夠提高所需的所有指標。


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;

    }

}


查看完整回答
反對 回復 2022-12-24
?
夢里花落0921

TA貢獻1772條經驗 獲得超6個贊

您可以存儲平均值,然后在游戲結束后重新計算。這樣你就不需要存儲一個會導致溢出問題的值(totalscore)(遲早)。


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;

    }

}


查看完整回答
反對 回復 2022-12-24
  • 2 回答
  • 0 關注
  • 101 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號