如何在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