在Java中求內接於正方形的圓的面積


圓是一個沒有角的二維平面圖形。每個圓都有一個圓心,圓上每一點到圓心的距離都相等。圓心到圓上任意一點的距離稱為圓的半徑。同樣,如果我們畫一條穿過圓心,連線圓上兩點的線段,這條線段稱為圓的直徑。基本上,直徑是半徑長度的兩倍。

正方形由四條邊組成,且四條邊的長度相等。如果我們嘗試將一個圓以最大的可能半徑放入正方形內,那麼圓的直徑等於正方形的邊長。因此,我們可以得出結論:圓的半徑等於正方形邊長的一半。

圓的面積是指圓所佔據的總表面積。我們可以使用半徑和一個稱為π的常數來計算圓的面積。

計算圓面積的公式:

$$\mathrm{面積=\pi \times (半徑)^{2}}$$

由於圓內接於正方形,則圓的半徑 (r) = 邊長/2 其中,“邊長”指的是正方形的邊長。

$$\mathrm{內接於正方形的圓的面積=\varpi\times(邊長/2)^{2}=\varpi\times(邊長^{2}/4)=(\varpi/4)^{*}邊長^{2}}$$

在這篇文章中,我們將學習如何使用Java計算內接於正方形的圓的面積。

以下是一些示例:

示例1

The side length of the square given = 9
The area of the circle inscribed in square = (ϖ / 4) * side2 = (3.141/4) * 9 * 9 = 63.605

示例2

The side length of the square given = 50
The area of the circle inscribed in square = (ϖ / 4) * side2 = (3.141/4) * 50 * 50 =
1963.125

示例3

The side length of the square given = 32
The area of the circle inscribed in square = (ϖ / 4) * side2 = (3.141/4) * 32 * 32 = 804.096

演算法

步驟1 - 獲取正方形的邊長,可以透過靜態輸入或使用者輸入。

步驟2 - 使用公式計算內接於正方形的圓的面積。

步驟3 - 列印結果。

多種方法

我們提供了多種不同的方法來解決這個問題。

  • 使用靜態輸入值。

  • 使用帶有靜態輸入值的使用者自定義方法。

  • 使用帶有使用者輸入值的使用者自定義方法。

讓我們逐一檢視程式及其輸出。

方法1:使用靜態輸入值

在此方法中,我們宣告一個雙精度變數,並用正方形的邊長對其進行初始化。然後,使用演算法可以找到內接於正方形的圓的面積。

示例

import java.io.*;
public class Main {
   //main method
   public static void main (String[] args) {
      //declare a variable to store the value of pi
      double pi = 3.14;
      //declare a variable to store the value of side of the square
      float side = 15;
      //declare a variable to store the area of the circle
      //find area by using the formula
      double area = ( pi / 4 ) * side * side;
      System.out.println("Area of the circle inscribed in the square is: "+ area);
   }
}

輸出

Area of the circle inscribed in the square is: 176.625

方法2:使用帶有靜態輸入值的使用者自定義方法

在此方法中,我們宣告一個雙精度變數,並初始化正方形的邊長值。然後,使用演算法可以找到內接於正方形的圓的面積。

示例

import java.io.*;
public class Main {
   //declare a static variable to store the value of pi
   static double pi = 3.14;

   //main method
   public static void main (String[] args) {
      //declare a variable to store the value of side of the square
      float side = 15;
      System.out.println("Area of the circle inscribed in the square is: "+ areaOfCircle(side));
   }

   // user-defined method to find the area of the circle
   static double areaOfCircle(float side) {
      return ( pi / 4 ) * side * side;
   }
}

輸出

Area of the circle inscribed in the square is: 176.625

方法3:使用帶有使用者輸入值的使用者自定義方法

在此方法中,我們宣告一個雙精度變數,並獲取使用者輸入的正方形邊長。然後,使用演算法可以找到內接於正方形的圓的面積。

示例

import java.io.*;
import java.util.*;
public class Main {
   //declare a static variable to store the value of pi
   static double pi = 3.14;

   //main method
   public static void main (String[] args) {
      //Create object of Scanner class
      Scanner sc= new Scanner(System.in);
      System.out.print("Enter the length of side of the square: ");
      //declare a variable to store the value of side of the square
      double side = sc.nextDouble();
      System.out.println("Area of the circle inscribed in the square is: "+
      areaOfCircle(side));
   }

   // user-defined method to find the area of the circle
   static double areaOfCircle(double side) {
      return ( pi / 4 ) * side * side;
   }
}

輸出

Enter the length of side of the square: 9
Area of the circle inscribed in the square is: 63.585

在這篇文章中,我們探討了如何使用不同的方法在Java中計算內接於正方形的圓的面積。

更新於:2022年12月27日

瀏覽量:553

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.