JS计算圆的周长和面积 叁歲伎倆 2023-10-06 08:19 6阅读 0赞 ## 题目: 计算圆的周长和面积 ## 最近刚学习js,就把书上的习题做一下。 <!DOCTYPE html > <html> <head> </head> <body> <!--计算圆的周长和面积--> <div> <p>周长:<input id="zc" type="text" value="周长" ></p> <p>面积:<input id="mj" type="text" value="面积" ></p> </div> <script> var a = parseFloat(prompt("请输入圆的半径:")) const p = 3.14 //因为Π是不变的,所以我把它设为常量 var b = 2*p*a//周长 var c = a*p*a//面积 if(Boolean(a)){ //判断是否为空 if( isNaN(a)){ //判断是否为数值 alert("输入错误") } else{ document.getElementById("zc").value = b.toFixed(2)//toFixed(2)化简小数点后两位 document.getElementById("mj").value = c.toFixed(2) } } </script> </body> </html> 运行结果: ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDc5NzUzOQ_size_16_color_FFFFFF_t_70] ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDc5NzUzOQ_size_16_color_FFFFFF_t_70 1] 小白的思路和方法,简单就好 ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDc5NzUzOQ_size_16_color_FFFFFF_t_70_pic_center] [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDc5NzUzOQ_size_16_color_FFFFFF_t_70]: https://img-blog.csdnimg.cn/20200507220300161.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDc5NzUzOQ==,size_16,color_FFFFFF,t_70 [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDc5NzUzOQ_size_16_color_FFFFFF_t_70 1]: https://img-blog.csdnimg.cn/202005072203006.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDc5NzUzOQ==,size_16,color_FFFFFF,t_70 [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDc5NzUzOQ_size_16_color_FFFFFF_t_70_pic_center]: https://img-blog.csdnimg.cn/20200507220612202.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDc5NzUzOQ==,size_16,color_FFFFFF,t_70#pic_center
还没有评论,来说两句吧...