金牌笔试题 原老师版
1. 机试题
定义一个班级类,并定义相关的类成员。
public class Grade
要求班级类中包含一个泛型集合,用于存放学生集合。
List<Student> list=new List<Student>();
定义该类中的一个方法(DisplayStudent):要求:公有的,有一个Point类型的形参,
其功能是能以形参指定的位置为起点,一字排开显示所有的学生对象。
书写一个AddStudent()用于添加学生
需要动态生成控件喔!!!
代码如下:
//Grade类
using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Exam2 { public class Grade { List<Student> list = new List<Student>(); public List<string> DisplayStudent(Point point) { int count = 0; List<string> newList = new List<string>(); foreach (Student item in list) { count++; newList.Add(item.StuName + ":" + (point.X + 80 * count) + ":" + (point.Y + 50)); } return newList; } //添加学生 public void AddStudent(Student stu) { list.Add(stu); } } }
//student类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Exam2 { public class Student { public string StuName { get; set; }//学生姓名 } }
//主代码
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Exam2 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { Grade grade = new Grade(); Student s1 = new Student(); s1.StuName = "甄子丹"; Student s2 = new Student(); s2.StuName = "成龙"; Student s3 = new Student(); s3.StuName = "李小龙"; Student s4 = new Student(); s4.StuName = "吴京"; Student s5 = new Student(); s5.StuName = "洪金宝"; Student s6 = new Student(); s6.StuName = "元彪"; grade.AddStudent(s1); grade.AddStudent(s2); grade.AddStudent(s3); grade.AddStudent(s4); grade.AddStudent(s5); grade.AddStudent(s6); List<string> list=grade.DisplayStudent(new Point(){X=1,Y=1}); foreach (string item in list) { string[] temp = item.Split(':'); string t1 = temp[0]; int x = Convert.ToInt32(temp[1]); int y = Convert.ToInt32(temp[2]); Button btn = new Button(); btn.Text = t1; btn.Size = new System.Drawing.Size(60, 25); btn.TextAlign = ContentAlignment.MiddleCenter; //设置位置 btn.Location = new Point(20+x , 30+y ); this.Controls.Add(btn); } } } }