第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何存儲(chǔ)課堂平均分和最高分?

如何存儲(chǔ)課堂平均分和最高分?

C#
蕭十郎 2022-12-24 10:00:06
我不知道如何在我的 user.cs 類中存儲(chǔ)玩家的平均分?jǐn)?shù)、高分和完成游戲的平均時(shí)間。每當(dāng)玩家完成我的一輪游戲時(shí),平均分和最高分以及平均時(shí)間都必須在他們的標(biāo)簽中每次更新。我試過(guò)使用數(shù)組和數(shù)組列表,但我仍然不確定,因?yàn)樗鼈兯坪醵疾黄鹱饔谩_@是我的 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;        }    }我還需要在標(biāo)簽中顯示用戶名、用戶名、高分、平均分和時(shí)間。格式應(yīng)為雙精度/浮點(diǎn)數(shù)。
查看完整描述

2 回答

?
MYYA

TA貢獻(xiàn)1868條經(jīng)驗(yàn) 獲得超4個(gè)贊

由于平均值的工作方式,您無(wú)法存儲(chǔ)平均分?jǐn)?shù)。雖然您可以通過(guò)每次游戲結(jié)束時(shí)將計(jì)數(shù)器簡(jiǎn)單地增加一個(gè)來(lái)計(jì)算用戶玩過(guò)的游戲,但是沒(méi)有分析形式來(lái)提高平均值。


但是,如果您存儲(chǔ)了游戲總數(shù)和總得分,那么您將能夠提高所需的所有指標(biāo)。


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;

    }

}


查看完整回答
反對(duì) 回復(fù) 2022-12-24
?
夢(mèng)里花落0921

TA貢獻(xiàn)1772條經(jīng)驗(yàn) 獲得超6個(gè)贊

您可以存儲(chǔ)平均值,然后在游戲結(jié)束后重新計(jì)算。這樣你就不需要存儲(chǔ)一個(gè)會(huì)導(dǎo)致溢出問(wèn)題的值(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;

    }

}


查看完整回答
反對(duì) 回復(fù) 2022-12-24
  • 2 回答
  • 0 關(guān)注
  • 98 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)