C#ArrayList集合——小例題

來源:互聯網
上載者:User

標籤:

用兩種方法編寫題目:輸入姓名,語文分數,數學分數,英語分數到集合,求語文總分,數學平均分,英語最高分以及英語最高分是誰。
法一:一個集合的做法
Console.Write("輸入總人數:");
int s = int.Parse(Console.ReadLine());
ArrayList ss = new ArrayList();
for (int i = 0; i < s; i++)
{
Console.Write("第" + (i + 1) + "的姓名:");
ss.Add(Console.ReadLine());
Console.Write("第" + (i + 1) + "英語成績:");
ss.Add(Console.ReadLine());
Console.Write("第" + (i + 1) + "數學成績:");
ss.Add(Console.ReadLine());
Console.Write("第" + (i + 1) + "語文成績:");
ss.Add(Console.ReadLine());
}
for (int i = 1; i <= 4 * s; i += 4)
{
for (int j = i; j <= 4 * s - 4; j += 4)
{
if (double.Parse(ss[i].ToString()) < double.Parse(ss[j + 4].ToString()))
{
double ying = double.Parse(ss[i].ToString());
ss[i] = ss[j + 4];
ss[j + 4] = ying;
string xing = ss[i - 1].ToString();
ss[i - 1] = ss[j + 3];
ss[j + 3] = xing;
double shu = double.Parse(ss[i + 1].ToString());
ss[i + 1] = ss[j + 5];
ss[j + 5] = shu;
double yu = double.Parse(ss[i + 2].ToString());
ss[i + 2] = ss[j + 6];
ss[j + 6] = yu;
}
}
}
for (int i = 0; i < 4 * s; i++)
{
Console.WriteLine(ss[i]);
}
double sum1 = 0;
double sum2 = 0;
for (int i = 0; i < 4 * s; i+=4)
{
sum1 += double.Parse(ss[i + 2].ToString());
sum2 += double.Parse(ss[i + 3].ToString());
}
Console.WriteLine("數學總分:" + sum1);
Console.WriteLine("語文平均分:" + (sum2 / s));
Console.WriteLine("英語分數最高的人是:" + ss[0] + ",分數是:" + ss[1]);
Console.ReadLine();
法二:四個集合的做法
Console.Write("輸入總人數:");
int s = int.Parse(Console.ReadLine());
ArrayList ss = new ArrayList();
ArrayList rr = new ArrayList();
ArrayList tt = new ArrayList();
ArrayList yy = new ArrayList();
for (int i = 0; i < s; i++)
{
Console.Write("第" + (i + 1) + "的姓名:");
ss.Add(Console.ReadLine());
Console.Write("第" + (i + 1) + "英語成績:");
rr.Add(Console.ReadLine());
Console.Write("第" + (i + 1) + "數學成績:");
tt.Add(Console.ReadLine());
Console.Write("第" + (i + 1) + "語文成績:");
yy.Add(Console.ReadLine());
}
for (int i = 0; i < s; i++)
{
for (int j = i; j < s - 1; j++)
{
if (double.Parse(rr[i].ToString()) < double.Parse(rr[j + 1].ToString()))
{
double ying = double.Parse(rr[i].ToString());
rr[i] = rr[j + 1];
rr[j + 1] = ying;
string xing = ss[i].ToString();
ss[i] = ss[j + 1];
ss[j + 1] = xing;
double shu = double.Parse(tt[i].ToString());
tt[i] = tt[j + 1];
tt[j + 1] = shu;
double yu = double.Parse(yy[i].ToString());
yy[i] = yy[j + 1];
yy[j + 1] = yu;
}
}
}
for (int i = 0; i < s; i++)
{
Console.WriteLine(ss[i] + "\t");
Console.WriteLine(rr[i] + "\t");
Console.WriteLine(tt[i] + "\t");
Console.WriteLine(yy[i] + "\t");
}
double sum1 = 0;
double sum2 = 0;
for (int i = 0; i < s; i++)
{
sum1 += double.Parse(tt[i].ToString());
sum2 += double.Parse(yy[i].ToString());
}
Console.WriteLine("數學總分:" + sum1);
Console.WriteLine("語文平均分:" + (sum2 / s));
Console.WriteLine("英語分數最高的人是:" + ss[0] + ",分數是:" + rr[0]);
Console.ReadLine();

C#ArrayList集合——小例題

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.