▄︻┻┳═一菜鸟求教个题目,急!!
输入10个学生5门课的成绩,分别用函数求:
1。每个学生的平均分,每门课的平均分??
2。找出最高的分数所对应的学生和课程??
用vbscript或javascript解都可以,小弟菜鸟,一直不知道怎么做。
望各位大虾给个讲解,答案也成,感激不尽!!!
先在此谢过!!
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<script type="text/javascript">
var courses={
$__courses__:[],
$__number__:0,
list:function() {
return this.$__courses__;
},
count:function() {
return this.$__number__;
},
append:function(name) {
this.$__courses__.push({name:name,sum:0});
this.$__number__++;
},
indexOf:function(idx) {
return this.$__courses__[idx];
}
}
var student=function(name) {
var $__name__=(name==null)?'':name;
var $__courses__=[];
this.name=function(name) {
if (name!=null)
$__name__=name;
else
return $__name__;
};
this.courses=function() {
return $__courses__;
};
this.inputCourse=function() {
for (var i=0;i<courses.count();) {
mark=prompt('请输入'+$__name__+'同学'+courses.indexOf(i).name+'课程的成绩:','0');
if (mark) {
$__courses__.push({
name:courses.indexOf(i).name,
mark:parseFloat(mark)
});
courses.indexOf(i).sum+=parseFloat(mark);
i++;
}
}
};
}
var statistic=function() {
var $__students__=[];
this.append=function(student) {
$__students__.push(student);
};
this.avgForStudent=function() {
var avg=[];
for (var i=0;i<$__students__.length;i++) {
var student=$__students__[i];
var courses=student.courses();
var sum=0;
for (var j=0;j<courses.length;j++) {
sum+=courses[j].mark;
}
avg.push({
name:student.name(),
avg:(sum/courses.length)
});
}
return avg;
};
this.avgForCourse=function() {
var avg=[];
for (var i=0;i<courses.count();i++) {
avg.push({
name:courses.indexOf(i).name,
avg:(courses.indexOf(i).sum/$__students__.length)
});
}
return avg;
};
this.max=function() {
var max=[];
var maxMark=0;
for (var i=0;i<$__students__.length;i++) {
var student=$__students__[i];
var course=student.courses();
for (var j=0;j<course.length;j++) {
if (course[j].mark>maxMark) {
maxMark=course[j].mark;
}
}
}
for (var i=0;i<$__students__.length;i++) {
var student=$__students__[i];
var course=student.courses();
for (var j=0;j<course.length;j++) {
if (course[j].mark==maxMark) {
max.push({
student_name:student.name(),
course_name:course[j].name,
mark:maxMark
});
}
}
}
return max;
};
}
</script>
<script type="text/javascript">
window.onload=function() {
var i;
var course_name,student_name,stat;
alert('请输入所要统计的课程');
i=1;
while (course_name=prompt('课程'+i.toString()+'的名称(终止输入,请点取消)','')) {
courses.append(course_name);
i++;
}
alert('请输入所要统计的学生');
stat=new statistic();
i=1;
while (student_name=prompt('学生'+i.toString()+'的姓名(终止输入,请点取消)','')) {
var stu=new student(student_name);
stu.inputCourse();
i++;
stat.append(stu);
}
var avg,max,str;
avg=stat.avgForStudent();
str='学生的平均成绩:\n';
for (var i=0;i<avg.length;i++) {
str+=avg[i].name+':'+avg[i].avg+'\n';
}
alert(str);
avg=stat.avgForCourse();
str='课程的平均成绩:\n';
for (var i=0;i<avg.length;i++) {
str+=avg[i].name+':'+avg[i].avg+'\n';
}
alert(str);
max=stat.max();
str='获得最高分数的学生以及课程:\n';
for (var i=0;i<max.length;i++) {
str+=max[i].student_name+','+max[i].course_name+':'+max[i].mark+'\n';
}
alert(str);
}
</script>
<title></title>
</head>
<body>
</body>
</html>