for/while

作者: Taoqi思 | 来源:发表于2018-11-07 18:41 被阅读0次

    for

    <!DOCTYPE html>
    <html lang="en">
    <head><meta charset="UTF-8">
    <title>for</title>
    <script>
        var sum = 0;
        for(var odd=1;odd < 100; odd++){
            if(odd%2!=0){
                sum+=odd;
            }
        }
        alert('1-100之间所有奇数之和:'+sum);
        var b7s = 0;
        var b7n = 0;
        for (var i = 1;i<100;i++) {
            if (i%7==0) {
                b7s+=i;
                b7n+=1;
            }
        }
        alert('1-100之间所有7的倍数的总和:'+b7s+',1-100之间所有7的倍数的个数:'+b7n);
        for (var i=100; i<1000; i++) {
            var ge = i%10;
            var shi = (parseInt(i/10))%10;
            var bai = parseInt(i/100);
            if(ge**3+shi**3+bai**3==i){
                console.log('水仙花数:'+i);
            }
        }
        var iszhi = prompt('输入一个数:');
        if(!(+iszhi)|| iszhi=='' || +iszhi<2 || +iszhi!=parseInt(iszhi)){
                alert('输入有误。');
        }
        else{
            var flag=true;
            for (var i = 2; i < +iszhi/2; i++) {
                if (+iszhi%i==0) {
                    flag=false;
                }
            }
            if (flag){
                alert('是质数');
            }
            else{
                alert('不是质数');
            }
        }
        var str='';
        for (var i = 1; i <=9 ; i++) {
            for (var col = 1; col <= i; col++) {
                str += (col+'*'+i+'='+col*i+'  ');
            }
            str += '\n';
        }
        console.log(str);
        var zhi='';
        for (var i = 2; i <= 100; i++) {
            var flag=true;
            for (var b = 2; b <= i/2; b++) {
                if (i%b==0) {
                    flag=false;
                    break;
                }
            }
            if (flag){
                zhi += i+' ';
            }
        }
        console.log(zhi);
    </script>
    </head>
    <body>
    </body>
    </html>
    
    for.png

    while:

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>while</title>
    <script>
        var moy = 1000;
        var n = 0;
        while(moy < 5000){
            moy += moy*0.05;
            n+=1;
        }
        alert(n+'年');
        do{
            var score = prompt('小明成绩:');
        }while(!(+score>=0&&+score<=100&&score!=''));
    </script>
    </head>
    <body>
    </body>
    </html>
    
    while.png

    相关文章

      网友评论

          本文标题:for/while

          本文链接:https://www.haomeiwen.com/subject/rhrdxqtx.html