2017-3-4 练习总结

1,抽奖代码(用集合for循环)

 List<string> uname = new List<string>();
            uname.Add("小王");
            uname.Add("小名");
            uname.Add("小李");
            uname.Add("小张");
            uname.Add("小毛");
            uname.Add("小徐");

            List<string> jp = new List<string>();
            jp.Add("洗衣机");
            jp.Add("冰箱");
            jp.Add("电脑");
            jp.Add("美女");
            jp.Add("豪车");
            jp.Add("别墅");

            for (int i = 0; i < 50;i++ )
            {
                Console.Clear();
                Random r = new Random();
                string aa = uname[r.Next(0, uname.Count+1)];
                string bb = jp[r.Next(0, jp.Count+1)];

                Console.WriteLine("恭喜" + aa + "抽中了" + bb + "奖品");
                System.Threading.Thread.Sleep(50);

            }
            Console.Clear();//清除上面内容
            Console.WriteLine("恭喜小徐抽中了豪车");//可以显示客户任意结果
            
            Console.ReadLine();

2.按提示输入学生信息,并按照学生成绩进行排名,并判断学生出入的生日是否正确,并且输出学生的年龄。(用冒泡排序,给学生成绩进行排序),

Console.Write("请输入学生的个数:");
            int nub = Convert.ToInt32(Console.ReadLine());

            List<student> list = new List<student>();
            student ss = new student();
            for (int i = 0; i < nub;i++ )
            {

                
                Console.Write("请输入第"+(i+1)+"个学生的学号:");
                ss.id = Console.ReadLine();

                Console.Write("请输入第" + (i + 1) + "个学生的姓名:");
                ss.name = Console.ReadLine();
                for (; ; )
                {
                    try
                    {
                        Console.Write("请输入第" + (i + 1) + "个学生的生日:");
                        ss.birth = Convert.ToDateTime(Console.ReadLine());
                        break;

                    }
                    catch
                    {
                        Console.WriteLine("您输入的生日不对,请重新输入");
                    }
                }


                Console.Write("请输入第" + (i + 1) + "个学生的成绩:");
                ss.score =Convert.ToDouble( Console.ReadLine());

                list.Add(ss);

               
            }
           
            Console.WriteLine("======================学生信息展示===========================");
            for (int i = 0; i < nub; i++)//冒泡排序
            {
                for (int j = i+1; j < nub; j++)
                {
                    if (list[i].score < list[j].score)
                    {
                        student aa = list[i];
                        list[i] = list[j];
                        list[j] = aa; 
                    }
                }
            }
           

            foreach(student s in list)
            {
                int age = DateTime.Now.Year - s.birth.Year;
                Console.WriteLine("学号:"+s.id+"\t姓名:"+s.name+"\t生日:"+s.birth.Year+""+s.birth.Month+""+s.birth.Day+""+"\t年龄:"+age+"\t年龄成绩:"+s.score+"\t");
            }


            Console.ReadLine();
        }

 

posted @ 2017-03-04 14:48  青年a  阅读(158)  评论(0编辑  收藏  举报