投票選班長

來源:互聯網
上載者:User

標籤:

 while (true)             {

                Console.WriteLine("請為五位同學開始投票(0/1/2/3/4):");                 int[] vote = new int[5];//給五個人投票,定義一個長度為5的數組,每一個下標代表一個人                 for (int i = 0; i < 30; i++)//30人投票,每人一票,迴圈30次                 {                     //票數代入                     Console.WriteLine("請第" + (i + 1) + "位同學投票(0-4):");//下標從0開始,給五個人投票                     int t = Convert.ToInt32(Console.ReadLine());                     if (t < 0 || t > 4)                     {                         Console.WriteLine("廢票請重新再投");                         continue;//廢票包含在內                     }                     else                     {                         vote[t]++;                         //投出的票記入票數中                     }                 }//計算最終得票                 int max = 0, maxsub = 0;//定義初始值為0,max為得到最高票,//maxsub記錄最大值得下標                 for (int i = 0; i < 5; i++)                 {//把每個人的票數顯示出來                     Console.WriteLine("第" + (i) + "的候選人的票數是" + vote[i] + "票");                     if (vote[i] > max)//計算最大值                     {                         max = vote[i];                         maxsub = i;//最大值的下標位置                     }

                }//顯示最終結果                 Console.WriteLine("最終投票結果為:" + maxsub + "當選,當選票數為" + max + "票");             }             Console.ReadLine();

投票選班長

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.