Question: in order to complete the statistical work of the students' information in a class, we need to abstract the students into a certain category, and then use an array to save the students' information, and output the students' information (Chinese score, computer score, English score, name, gender, student number) according to the requirements:
Output students' average scores from high to low
All boys' names will be output
Output all girl student numbers
Get an average
Let's say there are 10 students in the class
The code is as follows:
Option explicit 'Define course category Class course Private cName 'Full name Private cScore 'achievement 'Property Get，Property Let，Property Set Read values, assign values, and object types. Public property get courseName courseName =cName 'Read value and return value End Property Public property let courseName( vName) cName =vName 'No return value for variable assignment End Property 'Pay attention to the process of assignment of course grade Public property get courseScore courseScore =cScore 'Read value End Property Public property let courseScore( vs) cScore =vs 'Assignment of variables End Property End Class 'Define student class Class student Private sName 'Student name Private sgender 'Student gender Private sNum 'Student ID Private lstCourses 'curriculum 'Property Get，Property Let，Property Set Read values, assign values, and object types. Public property get averScore Dim avertmp,ln,aver,i 'Define variable total score, number of courses, average base avertmp=0 ln = UBound(lstCourses) 'UBOUND Function returns the maximum subscript of an array. The data type is Long For i =0 to ln-1 avertmp= avertmp+ lstCourses(i).courseScore Next If ln>1 Then aver= ln else aver=1 End If averScore= avertmp/aver End Property Public property get studName studName= sName 'Read value End Property Public property let studName(sv) sName=sv 'assignment End Property Public property get studNum studNum= sNum 'Read value End Property Public property let studNum(sv) sNum=sv 'assignment End Property Public property get studGender studGender= sgender 'Read value End Property Public property let studGender(sv) sgender=sv 'assignment End Property Public sub setSelcCourses(sv) ''statement Sub The name of the procedure, its parameters, and the code that makes up its body. Dim ln,i ln = UBound(sv) set lstCourses=nothing redim lstCourses(ln) 'ReDim Statement is used at the procedure level to reallocate storage space for dynamic array variables. For i=0 to ln-1 set lstCourses(i)= sv(i) 'Object type assignment Next End sub End Class 'The test code test sample is only for the information processing of one student. If it is for all students in the class, please complete the corresponding code Dim a,j,e,f,g,bb,i,c,m,nn,ss,ni,si Dim n,x,s Dim ac(3,10) Dim b(10),b2(10) Dim d(3) Dim n1(),s1() ReDim n1(10),s1(10) c = "" nn = "" ss = "" e=Array(91,89,88,78,85,64,92,85,90,75) 'Chinese scores of ten students f=Array(91,89,88,78,85,64,92,85,90,75) 'Math scores of ten students g=Array(91,89,88,78,85,64,92,85,90,75) ' Computer scores of ten students n = Array("n1","n2","n3","n4","n5","n6","n7","n8","n9","n10")' Names of ten students x = Array("male","male","fmale","male","male","fmale","fmale","male","male","male") s = Array("1110","1111","1112","1113","1114","1115","1116","1117","1118","1119")' Student numbers of ten students For j =0 to 9 set b(j)= new course Next For j =0 to 9 b(j).courseName ="Chinese" b(j).courseScore = e(j) Next For j =0 to 9 set ac(0,j) = b(j) set b(j) = nothing 'release b(Object) Next For j =0 to 9 set b(j)= new course Next For j =0 to 9 b(j).courseName ="math" b(j).courseScore = f(j) Next For j =0 to 9 set ac(1,j) = b(j) set b(j) = nothing 'release b(Object) Next For j =0 to 9 set b(j)= new course Next For j =0 to 9 b(j).courseName ="computer" b(j).courseScore = g(j) Next For j =0 to 9 set ac(2,j) = b(j) set b(j) = nothing 'release b(Object) Next For j =0 to 9 Set b2(j) = new student Next ni =0 si = 0 For j =0 to 9 b2(j).studName = n(j) b2(j).studNum = s(j) b2(j).studGender = x(j) If b2(j).studGender = "male"Then n1(ni) = b2(j).studName ni = ni+1 else s1(si) =s(j) si=si+1 End If Set n(j) = nothing Set s(j) = nothing Set b2(j)=nothing Next Set bb=new student For j = 0 to 9 For i = 0 to 2 Set d(i)= new course set d(i) = ac(i,j) Next bb.setSelcCourses(d) b(j) = bb.averScore For i = 0 to 2 set d(i) = nothing 'release d(Object) Next Next 'b.studName& b.studNum& b.studGender&b.averScore& vbcrlf 'sort For i = 1 to 9 For j =1 to (10-i) If b(j)<b(j+1) Then m =b(j) b(j) = b(j+1) b(j+1)=m End If Next Next For i=1 to 10 c = c&" " &b(i) Next Dim length 'use length Represents the length of an array length = UBound(n1) - LBound(n1) + 1 For i=0 to length-1 nn = nn&" " &n1(i) Next length = UBound(s1) - LBound(s1) +1 For i=0 to length-1 ss = ss&" " &s1(i) Next msgbox "Male name:" &nn& vbNewLine & "Girl student ID:"&ss& vbNewLine & "Average:"&c For j = 0 to 9 Set b(j)=nothing Next
The above code basically realizes the function required by the problem, but in contrast, due to the long running time of too many cycles and assignments, and the output result does not consider the form of generating files, it is simply displayed in the form of a prompt box.
If you don't understand something, please refer to the w3c manual for advance understanding (this is also the experience below). If there is a better modification scheme, please leave a valuable note in the comment area. Thank you for your visit.