码迷,mamicode.com
首页 > Windows程序 > 详细

c#复习-1

时间:2016-06-16 23:15:27      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Collections;
 4 using System.Linq;
 5 using System.Text;
 6 using System.Threading.Tasks;
 7 
 8 namespace ConsoleApplication1
 9 {
10     class Program
11     {
12         struct student               //结构体
13         {
14             public int xuhao;
15             public string xuehao;       //学号
16             public string name;      // 姓名
17             public decimal fenshu;   //分数
18 
19         }
20         static void Main(string[] args)
21         {
22             //添加5个学生的信息到集合中,
23             //每个学生都有:学号,姓名,成绩,3个内容
24             //添加完毕后将学生的分数从高到低排列并打印出来
25 
26             //请输入第1个学生的学号:101
27             //请输入第1个学生的姓名:xxx
28             //请输入第1个学生的成绩:99
29             //------------------------(注意加个分割线)
30             //请输入第2个学生的学号:102
31             //请输入第2个学生的姓名:xxx
32             //请输入第2个学生的成绩:98
33             //....一直到第5个输入完,然后按回车
34             //第1个学生的学号:101,姓名:xxx:成绩:99
35             //第2个学生....
36             //第3个..
37             //每段都加个分割线
38 
39             //1、循环添加学生信息
40             ArrayList list = new ArrayList();
41 
42             for (int i = 1; i < 6; i++)
43             {
44                 student stu = new student(); //实例化
45 
46                 Console.Write("请输入第" + i + "个学生的学号:");
47                 stu.xuehao = Console.ReadLine();
48                 Console.Write("请输入第" + i + "个学生的姓名:");
49                 stu.name = Console.ReadLine();
50                 Console.Write("请输入第" + i + "个学生的成绩:");
51                 stu.fenshu = Convert.ToDecimal(Console.ReadLine());
52                 stu.xuhao = i;
53 
54                 list.Add(stu);
55                 Console.WriteLine("===============================");
56             }
57 
58             Console.WriteLine("------------------------成绩排序--------------------------");
59 
60             //2、排序
61 
62             for (int i = 0; i < list.Count - 1; i++)
63             {
64                 for (int j = i + 1; j < list.Count; j++)
65                 {
66                     student stu1 = (student)list[i];
67                     student stu2 = (student)list[j];
68 
69                     if (stu1.fenshu < stu2.fenshu)
70                     {
71                         Object ob = list[i];
72                         list[i] = list[j];
73                         list[j] = ob;
74                     }
75                 }
76             }
77 
78             //3、打印
79             foreach (object o in list)
80             {
81                 student ss = (student)o;
82                 Console.WriteLine("" + ss.xuhao + "个学生的学号:" + ss.xuehao + ",姓名:" + ss.name + ",分数:" + ss.fenshu + "");
83             }
84 
85 
86 
87 
88             Console.ReadLine();
89 
90 
91         }
92     }
93 }

技术分享

c#复习-1

标签:

原文地址:http://www.cnblogs.com/tonyhere/p/5592201.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!