Java 找到一个正方形内切圆的面积
圆是一个没有角的圆形二维图形。每个圆都有一个原点,圆上每一点到原点的距离都相等,这个距离被称为圆的半径。同样地,如果我们从圆的一边到另一边画一条线,并且原点位于其中间,那条线被称为圆的直径。基本上,直径是半径长度的两倍。
正方形由四个边组成,所有四个边的长度相等。如果我们尝试将一个半径最大的圆放在一个正方形内,那么圆的直径将等于正方形边长的长度。因此,我们可以得出结论:圆的半径等于正方形边长的一半。
圆的面积指的是圆所占据的总表面积。我们可以使用半径和称为π的常数来计算圆的面积。
计算圆的面积的公式为:
\mathrm{面积=\pi \times (半径)^{2}}
由于圆是内切正方形的,所以圆的半径(r)= 边长/2,其中“边长”指的是正方形的边长。
\mathrm{内切正方形内的圆的面积=\pi\times(边长/2)^{2}=\pi\times(边长^{2}/4)=(\pi/4)\times边长^{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中找到一个内接于正方形中的圆的面积。