<!DOCTYPE HTML>
<html >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>系好安全带,准备启航</title>
<script type="text/javascript">
//通过javascript的日期对象来得到当前的日期,并输出。
var mydate=new Date();
document.write(mydate.getFullYear()+"年");
document.write(mydate.getMonth()+1+"月");
document.write(mydate.getDate()+"日"+" ");
var weekday=["星期日","星期一","星期二","星期三","星期四","星期五","星期六"]
document.write(weekday[mydate.getDay()]);
//成绩是一长窜的字符串不好处理,找规律后分割放到数组里更好操作哦
var scoreStr = "小明:87;小花:81;小红:97;小天:76;小张:74;小小:94;小西:90;小伍:76;小迪:64;小曼:76";
var myarr=scoreStr.split(';');//按;符号进行数组分割
var sum=0;//总成绩
var average=0;//平均成绩
for(var i=0;i<myarr.length;i++)
{
var str=myarr[i].indexOf(':');
sum=sum+parseInt(mysrr[i].substr(str+1,2));
}
average=sum/myarr.length;
average=Math.floor(average);
document.write("--班级总分为: "+average);
//从数组中将成绩撮出来,然后求和取整,并输出。
</script>
</head>
<body>
</body>
</html>
看下我的代码:
<!DOCTYPE HTML>
<html >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>系好安全带,准备启航</title>
<script type="text/javascript">
function getScore() {
var mydate = new Date();
// 分别得到年月日
var year = mydate.getFullYear();
var month = mydate.getMonth();
var day = mydate.getDay();
var week = mydate.getDay();
// 年月日组成字符串
var ymd = year + "年" + month + "月" + day + "日";
var weekArr = ["星期日","星期一","星期二","星期三","星期四","星期五","星期六"];
document.write(ymd + " " + weekArr[week] + "<br/>");
var scoreStr = "小明:87;小花:81;小红:97;小天:76;小张:74;小小:94;小西:90;小伍:76;小迪:64;小曼:76";
// 按“;”进行分割得到数组
var scoreArr = scoreStr.split(";");
// 定义一个总分数
var sumScore = 0;
document.write("班级中分数分数如下:<br/>");
// 打印分数,并对每个人分数进行求和
for(var i=0;i<scoreArr.length;i++) {
var studentInfos = scoreArr[i];
var score = parseInt(studentInfos.split(":")[1]);
document.write(score + "<br/>");
sumScore += score;
}
document.write("班级总分数为:" + sumScore + "<br/>");
// 计算平均分数
var avgScore = sumScore / scoreArr.length;
// 对平均数保留整数
var newAvgScore = Math.round(avgScore);
// 弹框形式打印平均分数
alert(ymd + " " + weekArr[week] + "--班级平均分数为:" + newAvgScore);
document.write(ymd + " " + weekArr[week] + "--班级平均分数为:" + avgScore + "<br/>");
document.write(ymd + " " + weekArr[week] + "--班级平均分数为(四舍五入后):" + newAvgScore);
}
</script>
</head>
<body>
<from>
<input type="button" value="点击获取平均分数" onclick="getScore()" />
</from>
</body>
</html>
//通过javascript的日期对象来得到当前的日期,并输出
var getTime=new Date()
document.write(getTime,"<br />")
document.write("-----------------以下为中文日期","<br />")
document.write(getTime.getFullYear(),"年");
document.write(getTime.getMonth(),"月");
document.write(getTime.getDate(),"日","  ")
//创建存储中文日期的数组
var weekdays=["星期天","星期一","星期二","星期三","星期四","星期五","星期六"];
//获取日期数组的索引
var weeknum = getTime.getDay();
document.write(weekdays[weeknum],"--班级总分:");
//成绩是一长窜的字符串不好处理,找规律后分割放到数组里更好操作哦
var scoreStr = "小明:87;小花:81;小红:97;小天:76;小张:74;小小:94;小西:90;小伍:76;小迪:64;小曼:76";
var myarr = new Array();
myarr = scoreStr.split(";");
//从数组中将成绩撮出来,然后求和取整,并输出。
//利用循环将每一项相加,并存放在变量sum中。
var sum =0;
for(i=0;i<myarr.length;i++){
sum = sum + parseInt(myarr[i].slice(3));
}
//利用Math对象的取整方法对结果进行取整。
document.write(Math.round(sum/myarr.length));
多用浏览器的控制台
sum=sum+parseInt(mysrr[i].substr(str+1,2)); 这句中mysrr写错了,应该是myarr