首页 > 文章列表 > 使用给定的弧的宽度和高度计算圆的半径的JAVA程序

使用给定的弧的宽度和高度计算圆的半径的JAVA程序

计算 半径
241 2023-08-20

一个圆是一个没有角的圆形二维图形。每个圆都有一个起点,圆上的每个点与起点保持相等的距离。起点和圆上一点之间的距离被称为圆的半径。类似地,如果我们从圆的一边到另一边画一条线,并且起点位于中间,那条线被称为圆的直径。基本上,直径是半径长度的两倍。

圆弧是指圆的一部分或一段周长。简单来说,它是圆周上的一个开放曲线。

根据问题陈述,我们需要找到当弧的宽度和高度给定时的圆的半径。

用给定的弧宽和弧高来计算圆的半径的公式为 −

$$mathrm{r=w^{2}/8h+h/2}$$

其中,'r'是一个圆的半径。

‘h’ 是弧的高度,‘w’ 是弧的宽度。

所以,让我们继续看看如何使用Java编程语言来找到具有给定弧宽和弧高的圆的半径。

展示一些实例给你看−

Instance-1

的中文翻译为:

实例-1

Let say height (h) of arc = 2.
And width of (w) of arc = 4
Radius of circle by using given width and height of arc = 2

Instance-2

的中文翻译为:

实例-2

Let say height (h) of arc = 3.
And width of (w) of arc = 5
Radius of circle by using given width and height of arc = 2.54

Instance-3

的中文翻译为:

实例-3

Let say height (h) of arc = 1.
And width of (w) of arc = 4.
Radius of circle by using given width and height of arc = 2.5.

语法

要在Java中获得一个数的任意次幂,我们可以使用内置的 java.lang.Math.pow() 方法。

以下是使用方法获取2的幂的语法 -

double power = Math.pow (inputValue,2)

算法

步骤-1 − 通过静态输入或用户输入获取弧的宽度和高度。

步骤-2 − 通过使用公式找到圆的半径。

第三步 - 打印结果。

多种方法

我们以不同的方法提供了解决方案。

  • 通过使用静态输入值。

  • 通过使用用户定义的方法。

让我们逐个查看程序及其输出。

方法一:通过使用静态输入值

在这种方法中,我们声明两个双精度变量来保存弧的宽度和高度值,并在程序中初始化这些值。然后通过使用算法,我们可以找到圆的半径。

Example

的中文翻译为:

示例

public class Main {
   //main method
   public static void main(String[] args) {
      //width (w) and height (h) of arc
      double w = 5, h = 3;
      //find radius using the formula
      double r = ((w * w) / (8 * h) + h / 2);
      System.out.println( "Radius of the circle: "+r);
   }
 }

输出

Radius of the circle: 2.541666666666667

方法二:使用带有静态输入值的用户定义方法。

在这种方法中,我们声明两个双精度变量来保存弧的宽度和高度值,并将这些值作为用户输入。然后通过将这些值作为参数传递来调用一个用户定义的方法。然后在方法内部,通过使用算法,我们可以找到圆的半径。

Example

的中文翻译为:

示例

public class Main {
   //main method
   public static void main(String[] args) {
      //width (w) and height (h) of arc
      double w = 5, h = 2;
      //call the user defined method to get the radius
      findRadius(w, h);
   }

   //find radius of circle
   static void findRadius(double w, double h) {
      //find radius using the formula
      double r = ((w * w) / (8 * h) + h / 2);
      System.out.println( "Radius of the circle: "+r);
   }
}

输出

Radius of the circle: 2.5625

在本文中,我们探讨了如何使用不同的方法在Java中找到圆的半径,当圆的弧的宽度和高度已知时。