编程语言
首页 > 编程语言> > HTML 、Java script 求水仙花数,三位数单个求和,不被7整除的安全数,100以内奇数和,打印九九乘法表,球圆面积

HTML 、Java script 求水仙花数,三位数单个求和,不被7整除的安全数,100以内奇数和,打印九九乘法表,球圆面积

作者:互联网

三位数单个求和代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script type="text/javascript">
    var n1=Number(prompt("请输入一个三位整数:"));
      var a=parseInt( n1/100); b=parseInt((n1/10)%10); c=n1%10;
      var num=(a+b+c);
             alert("百位,十位,个位的和为:"+num);
    </script>
</body>
</html>

1~60以内不被7整除的所有安全数:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        var i;
        for(i=1;i<=60;i++){
          if ((i/7!=0)||(i%10!=7)){
            document.write(i+"、");
            }
        }
        </script>
</body>
</html>

求100~999的所有水仙花数:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        var i;
        for(i=100;i<=999;i++){
            var a=parseInt( i/100); b=parseInt((i/10)%10); c=i%10;
          if ((Math.pow(a,3))+(Math.pow(b,3))+(Math.pow(c,3))==i)
          {
            console.log(i+"、");
          }
        }
        </script> 
</body>
</html>

练习一:使用While循环计算出100以内奇数相加之和。

练习二:使用for循环打印99乘法表(在页面内输出可用“document.write(‘’);”)

100以内奇数和

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
<script>
   var i=1;
   var sum=0;
   while(i<=100){
       if((i%2)!=0)
       sum=sum+i;
       i++;
}
alert("100以内奇数和:"+sum)
</script>
</body>
</html>

打印九九乘法表:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
    for(i=1;i<=9;i++){
        for(j=1;j<=i;j++){
            document.write(j+"*"+i+"="+i*j+"  ")
        }
        document.write("<br>")
    }
</script>
</body>
</html>
  1. 练习一:编写函数,实现求圆面积
    从页面输入圆半径,在控制台中打印出圆面积。
  2. 练习二:编写一个判断某个非负整数是否能够同时被3,5,7整除的函数,然后在页面上输出1~1000之间所有能同时被3,5,7整除的整数
    提示:document.write()可在页面输出自定义内容

求圆面积,用自定义函数实现:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        var r=(prompt("请输入圆的半径:"));
        function area(a){
            return 3.14*a*a;
        }
        console.log("圆的面积为:"+area(r));
        </script> 
</body>
</html>

能同时被3、5、7整除的数,用自定义函数实现:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        function charge(i){
               if(i%3==0 && i%5==0 && i%7==0)
               return i; 
               else
               return 0;       
        }
        for(i=1;i<=1000;i++){
            if(charge(i)!=0){
            document.write(charge(i));
         document.write("、");
            }
        }        
        </script> 
</body>
</html>  

Java Script 套在HTML里面为网页设计动态效果,由于程序设计语言之间都是相似的,对应算法都一样,关键部分的代码做部分修改仁科适用于其他语言。

 

标签:九九乘法,Java,var,100,Document,圆面积,整除,页面
来源: https://blog.csdn.net/qq_53162555/article/details/117688623