关闭

js for循环的练习 99乘法表疯狂打印

1759人阅读 评论(0) 收藏 举报
分类:
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<script>

    // 3 求1-100之间所有数的和、平均值
        var sum = 0;
        for(var i=1;i<=100;i++){
            sum+=i;
        }
        var avg = sum/(i-1);
        console.log(avg);


   // 4 求1-100之间所有偶数的和、所有奇数的和
        var sumOdd = 0;//奇数和
        var sumEven = 0;//偶数和
        for(var i=1;i<=100;i++){
            if(i%2 === 1){ //判断如果是奇数加到sumOdd
                sumOdd += i;
            }else{         //如果是偶数加到sumEven
                sumEven += i;
            }
        }
        console.log("sumOdd"+sumOdd+"    sumEven="+sumEven);


    //本金10000元存入银行,年利率是千分之三,每过1年,将本金和利息相加作为新的本金。计算5年后,获得的本金是多少?
        var money = 10000;
        var rote = 0.003;
        for(var i=1;i<=5;i++){
            money *= 1+rote;
        }
        console.log(money);


    //斐波那契数列:1,1,2,3,5,8,13,21,34,55,89,144...........
    //需求:求斐波那契数列第12项是多少?
        var num1 = 1;
        var num2 = 1;
        for(var i=3;i<=12;i++){
            var temp = num2;
            num2 = num1+num2;
            num1 = temp;
        }
        console.log(num2);
//--------------------------------- 打印图形 -------------------------------

  <img src="http://img.blog.csdn.net/20160828200615767?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
 //需求:打印宽9,长9的正方形。
        for(var j=1;j<=9;j++){
            for(var i=1;i<=9;i++){
                document.write("☆");
            }
            document.write("<br>");
        }
        document.write("<br>");

 <img src="http://img.blog.csdn.net/20160828200738816?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" height="171" width="198" alt="" />
    //需求:打印三角形(外循环控制行,内循环控制列)
        for(var j=1;j<=9;j++){
            for(var i=1;i<=j;i++){
                document.write("☆");
            }
            document.write("<br>");
        }

 //---------------------------- 各种打印99乘法表 ----------------------

  <img src="http://img.blog.csdn.net/20160828201449878?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" height="200" width="455" alt="" />

       for(var l=1;l<=3;l++){
            for(var k=1;k<=3;k++){
                for(var i=1;i<=5;i++){
                    for(var j=1;j<=60;j++){
                        document.write("●");
                        if(j%5==0){
                            document.write("&nbsp;");
                        }
                        if(j%20==0){
                            document.write("&nbsp;&nbsp;&nbsp;&nbsp;");
                        }
                    }
                    document.write("</br>")
                }
                document.write("</br>")
            }
            document.write("</br><hr>")
        }

  <img src="http://img.blog.csdn.net/20160828201615501?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" height="200" width="471" alt="" />

        document.write("<table border='1'>");
        for(var i=1;i<=9;i++){
            document.write("<tr>")
            for(var j=1;j<=i;j++){
                document.write("<td>");
                document.write(i+"*"+j+"="+i*j);
                document.write("</td>");
            }
            document.write("</tr>")
            document.write("<br />")
        }
        document.write("</table>")
        document.write("<br /><hr><br />");

  <img src="http://img.blog.csdn.net/20160828201720379?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" height="232" width="540" alt="" />

        document.write("<table border='1' style='color: red;'>");
        var a=0;
        for(var i=9;i>=1;i--){
            document.write("<tr>")
            for(var m=0;m<a;m++){
                document.write("<td></td>");
            }
            a++;
            for(var j=i;j>=1;j--){
                document.write("<td>"+i+"*"+j+"="+i*j+"</td>");
            }
            document.write("</tr>")
        }
        document.write("</table>")
        document.write("<br /><hr><br />");

  <img src="http://img.blog.csdn.net/20160828201827817?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" height="235" width="600" alt="" />

        document.write("<table border='1'>");
        var a =9;
        for(var i=1;i<=9;i++){
            document.write("<tr>")
            for(var j=1;j<=i;j++){
                document.write("<td>"+i+"*"+j+"="+i*j+"</td>");
            }
            for(var k=a;k>=1;k--){
                document.write("<td style='color: red;'>"+a+"*"+k+"="+a*k+"</td>");
            }
            a--;
            document.write("</tr>")
        }
        document.write("</table>")

  <img src="http://img.blog.csdn.net/20160828201906177?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" height="388" width="258" alt="" />

        document.write("<table border='1'>");
        var a =9;
        for(var i=1;i<=9;i++){
            document.write("<tr>")
            for(var j=1;j<=i;j++){
                document.write("<td>"+"<br>"+i+"<br>"+"*"+"<br>"+j+"<br>"+"="+"<br>"+i*j+"</td>");
            }
            for(var k=a;k>=1;k--){
                document.write("<td style='color: red;'>"+a+"<br>"+"*"+"<br>"+k+"<br>"+"="+"<br>"+a*k+"</td>");
            }
            a--;
            document.write("</tr>")
        }
        document.write("</table>")
        document.write("<br /><hr><br />");

  <img src="http://img.blog.csdn.net/20160828201931379?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
        document.write("<table border='1'>");
        var a =9;
        for(var i=1;i<=9;i++){
            document.write("<tr>")
            for(var j=1;j<=i;j++){
                document.write("<td>");
                document.write(i+"*"+j+"="+i*j);
                document.write("</td>");
            }
            document.write("<td></td>	");
            for(var k=a;k>=1;k--){
                document.write("<td style='color: red;'>"+a+"*"+k+"="+a*k+"</td>");
            }
            a--;
            document.write("</tr>")
        }
        document.write("</table>")

</script>
</body>
</html>

1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:102681次
    • 积分:1937
    • 等级:
    • 排名:千里之外
    • 原创:102篇
    • 转载:30篇
    • 译文:2篇
    • 评论:4条
    最新评论