日期:2014-05-16  浏览次数:20562 次

各位大家 小弟初学java 有道题目请教各位 谢谢啦 越快越好啊
5门课程的成绩,以表格的形式输出,分为三列(课程名称、学时、成绩)
要求用JavaScript计算课程加权平均分,计算公式为:Σ课程成绩*学时数/总学时数

------解决方案--------------------

<html>
<head>
<title> 枚举字符串 </title>

<body>

<table border=1 id= "tb1 ">
<tr id= "tr1 "> <td align= "center "> 课程 </td> <td align= "center "> 学时 </td> <td align= "center "> 成绩 </td> </tr>
<tr id= "tr1 "> <td align= "center "> 课程1 </td> <td align= "center "> 60 </td> <td align= "center "> 85 </td> </tr>
<tr id= "tr1 "> <td align= "center "> 课程2 </td> <td align= "center "> 20 </td> <td align= "center "> 78 </td> </tr>
<tr id= "tr1 "> <td align= "center "> 课程3 </td> <td align= "center "> 30 </td> <td align= "center "> 98 </td> </tr>
<tr id= "tr1 "> <td align= "center "> 课程4 </td> <td align= "center "> 40 </td> <td align= "center "> 89 </td> </tr>
<tr id= "tr1 "> <td align= "center "> 课程5 </td> <td align= "center "> 50 </td> <td align= "center "> 70 </td> </tr>

</table>
<div id= "div1 "> <input type= "button " name= "btn1 " value= " 计算 " id= "btn1 "> </div>
<script language= "javascript ">
var trs = document.getElementById( "tb1 ");
var s=trs.lastChild.childNodes.length;
function trfocus(){this.style.background= "#CCCCCC ";}
function trblur(){this.style.background= " ";}
function window.onload()
{

if(trs.lastChild.hasChildNodes()){
var s=trs.lastChild.childNodes.length;

for(var i=0;i <s;i++){
trs.lastChild.childNodes[i].onmouseover=trfocus;
trs.lastChild.childNodes[i].onmouseout=trblur;
}
}
}
document.getElementById( "btn1 ").onclick=function(){
if(trs.lastChild.firstChild.nextSibling){
var fz=0,fm=0;
for(i=1;i <s;i++){
fm+=(trs.lastChild.childNodes[i].lastChild.previousSibling.innerText-0);
fz+=((trs.lastChild.childNodes[i].lastChild.innerText-0)*(trs.lastChild.childNodes[i].lastChild.previousSibling.innerText-0));
}
alert( "求得加权平均分为: "+fz/fm);
}
}
</script>
</body>
</html>
------解决方案--------------------
<script language= "javascript ">
var classes = new Array();
classes[classes.length] = [ "课程1 ",10,80];
classes[classes.length] = [ "课程2 ",12,70];
classes[classes.length] = [ "课程3 ",20,92];
classes[classes.length] = [ "课程4 ",16,84];
classes[classes.length] = [ "课程5 ",32,60];
var classescount = 0;
var count = 0;

for(var i=0;i <classes.length;i++)classescount+=classes[i][1];
for(var i=0;i <classes.length;i++)count+=classes[i][1]*classes[i][2]/classescount;
count = Math.round(count*10)/10;
var $C = function(value){return document.createElement(value);}
function window.onload()
{
createtable();
}
function createtable()
{
var oTable = $C( "table ");