问答详情
源自:7-23 编程练习

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

<!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循环注释掉之后会输出”--班级总分为“,但是实在找不到循环体的问题在哪里了。。。


提问者:卡溜灰 2016-05-11 16:08

个回答

  • 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>


  • 慕雪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-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-11 16:30:09

    应该是num 没有初始化

  • 稚气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>