如何在JavaScript中计算圆的面积和周长?
若要计算圆的面积和周长,可尝试运行以下代码 −
范例
<html>
<head>
<title>JavaScript Example</title>
</head>
<body>
<script>
function Calculate(r) {
this.r = r;
this.perimeter = function () {
return 2*Math.PI*this.r;
};
this.area = function () {
return Math.PI * this.r * this.r;
};
}
var calc = new Calculate(5);
document.write("Area of Circle = ", calc.area().toFixed(2));
document.write("<br>Perimeter of Circle = ", calc.perimeter().toFixed(2));
</script>
</body>
</html>
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP