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

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

尋找一種方法來縮短我的代碼

尋找一種方法來縮短我的代碼

C#
函數(shù)式編程 2021-06-03 11:32:50
這是我的代碼:    static void Main(string[] args)    {        Console.Write("How many tests would you like to do? 1 to 10: ");        int tests = Convert.ToInt32(Console.ReadLine());        Console.WriteLine();    }有人可以幫我解決我的代碼嗎?我不知道我在做什么
查看完整描述

1 回答

?
SMILET

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

制作一個(gè)數(shù)組int[] counts = new int[13]并使用counts[total]++;; 最后,循環(huán)它:


for(int i = 2 ; i <= 12 ; i++)

    // etc

(注意:您可以使用 11 個(gè)項(xiàng)目的數(shù)組并不斷處理兩個(gè),但是......這可能不值得)


就像是:


static void Main()

{


    Console.WriteLine("Investigation 1");

    Console.WriteLine("===============");

    Console.WriteLine();

    Console.Write("How many sets of tests? 1 to 10: ");


    int sets = Convert.ToInt32(Console.ReadLine());

    Console.WriteLine();


    Random r = new Random();

    int[] counts = new int[13];


    for (int ctr = 0; ctr < 36 * sets; ctr++)

    {

        int a = r.Next(1, 7), b = r.Next(1, 7), total = a + b;

        Console.WriteLine($"Roll {(ctr + 1)}: {a} +  = {total}");

        counts[total]++;

    }


    Console.WriteLine("=======================");

    Console.WriteLine();

    Console.WriteLine("Total  Expected  Actual");

    Console.WriteLine("=====  ========  ======");


    for(int i = 2; i <= 12; i++)

    {

        var expected = sets * (6 - Math.Abs(7 - i));

        Console.WriteLine($"  {i}        {expected}        {counts[i]}");

    }

}

對(duì)于直方圖:


var maxCount = counts.Max(); // needs "using System.Linq;" at the top

for (int i = 2; i <= 12; i++)

{

    var width = ((Console.WindowWidth - 10) * counts[i]) / maxCount; // make it proportional

    Console.WriteLine($"{i}\t{new string('*', width)}");

}


查看完整回答
反對(duì) 回復(fù) 2021-06-05
  • 1 回答
  • 0 關(guān)注
  • 143 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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