输出有问题,望能指正问题在那儿

来源:7-23 编程练习

卡溜灰

2016-05-11 16:08

<!DOCTYPE  HTML>

<html >

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>系好安全带,准备启航</title>


<script type="text/javascript">

    var today = new Date();

    var month = today.getMonth() + 1 +"月";  

    var week = ["星期一","星期二","星期三","星期四","星期五","星期六","星期日"];

    document.write(today.getFullYear()+"年"+month+today.getDate()+"日"+"&nbsp;"+week[today.getDay()]);

    var scoreStr = "小明:87;小花:81;小红:97;小天:76;小张:74;小小:94;小西:90;小伍:76;小迪:64;小曼:76";

    var scoreArr = scoreStr.split(";");

    

    var average;

    var num;

    for (var i=0;i<scoreArr.length;i++){

        var index = scoreArr[i].indexOf(':')+1;

        num += parseInt(scorArr[i].substr(index));

        average = num/soreArr.length;

        }

    document.write("--班级总分为:"+average);

</script>

</head>

<body>

</body>

</html>


现在只能正确输出日期,最后一个document.write语句的内容都不输出。试过将for循环注释掉之后会输出”--班级总分为“,但是实在找不到循环体的问题在哪里了。。。


写回答 关注

5回答

  • rambleHu
    2016-05-11 16:26:10
    已采纳

    注意单词和for循环平均分的位置

    <!DOCTYPE  HTML>

    <html>


    <head>

        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

        <title>系好安全带,准备启航</title>

        <script type="text/javascript">

        var today = new Date();

        var month = today.getMonth() + 1 + "月";

        var week = ["星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"];

        document.write(today.getFullYear() + "年" + month + today.getDate() + "日" + "&nbsp;" + week[today.getDay()]);

        var scoreStr = "小明:87;小花:81;小红:97;小天:76;小张:74;小小:94;小西:90;小伍:76;小迪:64;小曼:76";

        var scoreArr = scoreStr.split(";");


        var average=0;

        var num=0;

        for (var i = 0; i < scoreArr.length; i++) {

            var curNum = scoreArr[i].split(':') [1];

            num += parseInt(curNum);

        }

        average = num / scoreArr.length;

        document.write("--班级总分为:" + num);

        document.write("--班级总平均分:" + average);

        </script>

    </head>


    <body>

    </body>


    </html>


    卡溜灰

    非常感谢!

    2016-05-12 15:37:23

    共 1 条回复 >

  • 慕雪8793085
    2016-05-11 17:09:14

        var scoreArr = scoreStr.split(";");

        var average;

        var num;

        for (var i=0;i<scoreArr.length;i++){

            var index = scoreArr[i].indexOf(':')+1;

            num += parseInt(scorArr[i].substr(index));

            average = num/soreArr.length;

            }

    循环里面用的soreArr,你上面面定义的是scoreArr

    卡溜灰

    嗯,谢谢哈

    2016-05-12 15:38:37

    共 1 条回复 >

  • 扑那火火成灰灰
    2016-05-11 16:55:30

    <!DOCTYPE  HTML>

    <html >

    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    <title>系好安全带,准备启航</title>

    <script type="text/javascript">

        var today = new Date();

        var month = today.getMonth() + 1 +"月";  

        var week = ["星期一","星期二","星期三","星期四","星期五","星期六","星期日"];

        document.write(today.getFullYear()+"年"+month+today.getDate()+"日"+"&nbsp;"+week[today.getDay()]);

        var scoreStr = "小明:87;小花:81;小红:97;小天:76;小张:74;小小:94;小西:90;小伍:76;小迪:64;小曼:76";

        var scoreArr = scoreStr.split(";");

        

        var average;

        var num=0;

        for (var i=0;i<scoreArr.length;i++){

            var index = scoreArr[i].indexOf(':')+1;

            num += parseInt(scoreArr[i].substr(index));

            }

            average = num/scoreArr.length;

        document.write("--班级总分为:"+average);

    </script>

    </head>

    <body>

    </body>

    </html>


    额 ,我发现for循环里面scoreArr拼错两个,num要初始化,求平均分的代码位置不对,应该就酱

    http://img.mukewang.com/5732f3670001848403570075.jpg

    卡溜灰

    确实拼写错了,哈哈哈哈

    2016-05-12 15:38:22

    共 1 条回复 >

  • 刹那烟华丶
    2016-05-11 16:30:09

    应该是num 没有初始化

    卡溜灰

    嗯,谢谢

    2016-05-12 15:37:46

    共 1 条回复 >

  • 稚气De馒头
    2016-05-11 16:22:51

    <!DOCTYPE  HTML>

    <html >

    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    <title>系好安全带,准备启航</title>


    <script type="text/javascript">

        var today = new Date();

        var month = today.getMonth() + 1 +"月";  

        var week = ["星期一","星期二","星期三","星期四","星期五","星期六","星期日"];

        document.write(today.getFullYear()+"年"+month+today.getDate()+"日"+"&nbsp;"+week[today.getDay()]);

        var scoreStr = "小明:87;小花:81;小红:97;小天:76;小张:74;小小:94;小西:90;小伍:76;小迪:64;小曼:76";

        var scoreArr = scoreStr.split(";");



        var average;

       

    var num = 0;


        for (var i=0;i<scoreArr.length;i++){

            var index = scoreArr[i].indexOf(':')+1;

            num += parseInt(scoreArr[i].substr(index));

            average = num/scoreArr.length;

            }

        document.write("--班级总分为:"+average);

    </script>

    </head>

    <body>

    </body>

    </html>

    卡溜灰

    谢谢!xiexie!

    2016-05-12 15:39:09

    共 1 条回复 >

JavaScript进阶篇

本课程从如何插入JS代码开始,带您进入网页动态交互世界

468061 学习 · 21891 问题

查看课程

相似问题