Java 程序 - 求圆的周长
对于给定的半径为 "r" 的圆,编写一个 Java 程序求该圆的周长。周长 也称为圆的边缘长度。它是绕圆一周的距离。
周长 由公式C = 2𝜋r给出,其中,pi/𝜋 = 3.14,r 是圆的半径 −

示例情境
Input: radius = 5 Output: perimeter = 31.400000000000002
2 * 3.14 * 5 = 31.428571428571427
定义 PI 的常量
在此 Java 程序中,我们定义 PI 的值为常量,并使用它求圆的周长。
public class PerimeterOfCircle {
// defining constant
static final double PICONST = 3.14;
public static void main(String args[]) {
double my_radius, my_perimeter;
my_radius = 5;
System.out.println("The radius of the circle is defined as " +my_radius);
my_perimeter = PICONST * 2 * my_radius;
System.out.println("The perimeter of Circle is: " + my_perimeter);
}
}
输出
The radius of the circle is defined as 5.0 The perimeter of Circle is: 31.400000000000002
使用 Math.PI
在以下示例中,我们使用 Math.PI 来计算圆的周长。
public class PerimeterOfCircle {
public static void main(String args[]) {
double my_radius, my_perimeter;
my_radius = 7;
System.out.println("The radius of the circle is defined as " + my_radius);
my_perimeter = Math.PI * 2 * my_radius;
System.out.println("The perimeter of Circle is: " + my_perimeter);
}
}
输出
The radius of the circle is defined as 7.0 The perimeter of Circle is: 43.982297150257104
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP