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

更新於:2020 年 6 月 19 日

871 次瀏覽量

開啟你的 職業生涯

完成課程獲得認證

開始
廣告