Java程序查找圆的最长弦
圆是一个没有角的二维圆形图形。每个圆都有一个原点,圆上的每个点与原点保持相等的距离。原点与圆上一点之间的距离称为圆的半径。同样,如果我们从圆的一边到另一边画一条线,并且原点位于它的中间,那么这条线称为圆的直径。基本上,直径是半径长度的两倍。
圆的弦指的是从圆的一个端点到另一个端点的线。或者简单地说,弦指的是端点位于圆上的线。弦将圆分成两部分。
根据题意,我们需要找到圆的最长弦。很明显,穿过原点的线是最长弦,它就是直径。
如果给出半径 (r),则圆的直径为 2r。
因此,圆的最长弦为 2r。所以,让我们继续看看如何使用 Java 编程语言找到圆的最长弦。
举几个例子:
示例 1
Given radius (r) of the circle is 5 Then the longest chord of the circle = 2r = 2 * 5 = 10
示例 2
Given radius (r) of the circle is 4.5 Then the longest chord of the circle = 2r = 2 * 4.5 = 9
示例 3
Given radius (r) of the circle is 4 Then the longest chord of the circle = 2r = 2 * 4 = 8
算法
步骤 1 - 通过静态输入或用户输入获取圆的半径。
步骤 2 - 找到圆的直径,它也就是圆的最长弦。
步骤 3 - 打印结果。
多种方法
我们提供了不同方法的解决方案。
使用静态输入值。
使用带有静态输入值的用户定义方法。
使用带有用户输入值的用户定义方法。
让我们逐一查看程序及其输出。
方法 1:使用静态输入值
在这种方法中,我们声明一个双精度变量并用圆的半径值初始化它。然后,使用算法我们可以找到圆的最长弦。
示例
import java.util.*;
public class Main {
//main method
public static void main(String[] args) {
// radius of the circle
double r = 7;
//find the longest chord i.e diameter
double chord=2*r;
//print result
System.out.println("Length of longest chord of the circle:"+chord);
}
}
输出
Length of longest chord of the circle: 14.0
方法 2:使用带有静态输入值的用户定义方法
在这种方法中,我们声明一个双精度变量并用圆的半径值初始化它。通过将此半径值作为参数调用用户定义的方法。然后在方法内部使用算法找到圆的最长弦。
示例
public class Main {
//main method
public static void main(String[] args) {
//radius of the circle
double r = 4;
//call the user defined method to find longest chord
findLongestChord(r);
}
//user defined method to find longest chord
static void findLongestChord(double r) {
//find the longest chord i.e diameter
double chord=2*r;
//print result
System.out.println("Longest chord of the circle: "+chord);
}
}
输出
Longest chord of the circle: 8.0
方法 3:使用带有用户输入值的用户定义方法
在这种方法中,我们声明一个双精度变量并获取圆的半径值的用户输入。通过将此半径值作为参数调用用户定义的方法。然后在方法内部使用算法找到圆的最长弦。
示例
import java.util.*;
public class Main {
//main method
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter radius of circle: ");
//take user inout of radius of the circle
double r = sc.nextDouble();
//call the user defined method to find longest chord
findLongestChord(r);
}
//user defined method to find longest chord
static void findLongestChord(double r) {
//find the longest chord i.e diameter
double chord=2*r;
//print result
System.out.println("Length of longest chord of the circle: "+chord);
}
}
输出
Enter radius of circle: 4.5 Length of longest chord of the circle: 9.0
在本文中,我们探讨了如何使用不同的方法在 Java 中找到圆的最长弦。
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP