Java toRadians 函数

Java toRadians 函数是 Math 库函数之一,它将以度为单位测量的角度转换为以弧度为单位测量的近似等效角度。在本文中,我们将通过示例展示如何使用 Math.toRadians 函数,以及它在此编程语言中的语法是

static double toRadians(double number); //Return Type is Double

// To use in program: 
Math.toRadians(double number);

数字:它可以是您想要转换为弧度的角度(以度为单位)

Java toRadians 函数示例

Math.toRadians 函数可将度转换为以弧度为单位测量的近似等效角度。在此程序中,我们将查找正值和负值的弧度并将输出显示出来

package TrigonometricFunctions;

public class ToRadians {
	public static void main(String[] args) {
		double x = Math.toRadians(400.96 + 240.65 - 106.98);
		System.out.println("toRadians Function result =  " + x);	
		
		System.out.println("\nFinding Radians of Zero = " + Math.toRadians(0));
		
		System.out.println("\nFinding Radians of Positive Number = " + Math.toRadians(104.95));
		System.out.println("Finding Radians of Positive Number=  " + Math.toRadians(156.28));
		
		System.out.println("\nFinding Radians of Negative Number =  " + Math.toRadians(-135.85));
		System.out.println("Finding Radians of Negative Number =  " + Math.toRadians(-9.35));
	}
}
Java toRadians Function 1

首先,我们声明了一个 Double 类型的变量 x,并直接在表达式上使用了 Math.toRadians 函数。这里,我们使用 System.out.println 语句将弧度结果作为输出打印出来。

double x = Math.toRadians(400.96 + 240.65 - 106.98);
System.out.println("toRadians Function result =  " + x);

接下来,我们直接在正双精度值上使用了 Math.toRadians 函数。

System.out.println("\nFinding Radians of Positive Number = " + Math.toRadians(104.95));
System.out.println("Finding Radians of Positive Number=  " + Math.toRadians(156.28));

接下来,我们直接在负双精度值上使用了 toRadians Math 函数

System.out.println("\nFinding Radians of Negative Number =  " + Math.toRadians(-135.85));
System.out.println("Finding Radians of Negative Number =  " + Math.toRadians(-9.35));

Java toRadians 数组示例

在此 Java 程序中,我们将批量数据转换为弧度。 这里,我们将声明一个 double 类型的数组。接下来,我们使用 toRadians 函数将数组元素转换为弧度。

package TrigonometricFunctions;

public class ToRadiansOnArrays {
	public static void main(String[] args) {
		
		double [] myArray = {1, 30, 45, 60, 75, 90, 120, 180, 240, 360};

		for (int i = 0; i < myArray.length; i++) {
			System.out.format("Finding Radians of Array Element %.2f is = %.4f\n", myArray[i], Math.toRadians(myArray[i]));
		}
	}
}
Finding Radians of Array Element 1.00 is = 0.0175
Finding Radians of Array Element 30.00 is = 0.5236
Finding Radians of Array Element 45.00 is = 0.7854
Finding Radians of Array Element 60.00 is = 1.0472
Finding Radians of Array Element 75.00 is = 1.3090
Finding Radians of Array Element 90.00 is = 1.5708
Finding Radians of Array Element 120.00 is = 2.0944
Finding Radians of Array Element 180.00 is = 3.1416
Finding Radians of Array Element 240.00 is = 4.1888
Finding Radians of Array Element 360.00 is = 6.2832

我们使用For 循环来迭代数组。在 toRadians For 循环中,我们将 i 值初始化为 0。接下来,编译器将检查条件 (i < myArray.length)。

提示:myArray.length 用于查找数组的长度。

for (int i = 0; i < myArray.length; i++) {

这里,我们在 System.out.format 语句中直接使用了 Math.toRadians 函数。这意味着编译器将调用 Math.toRadians 方法( static double toRadians(double number) )来查找相应的弧度值。

System.out.format("Finding Radians of Array Element %.2f is = %.4f\n", myArray[i], Math.toRadians(myArray[i]));

注意:要查找单个项的弧度值,请使用:Math.toRadians(myArray[index_position])

Java toRadians ArrayList 示例

在此 Java 程序中,我们将声明一个 double 类型的 ArrayList 并查找列表元素的弧度值。

package TrigonometricFunctions;

import java.util.ArrayList;

public class ToRadiansOnArrayList {
	public static void main(String[] args) {
		ArrayList<Double> myList = new ArrayList<Double>(5);
		myList.add(Math.PI);
		myList.add(Math.PI / 3);
		myList.add(Math.PI / 4);
		myList.add(Math.PI / 6);
		myList.add(Math.PI / 9);
		
		for (double x : myList) {
			System.out.format("Finding Radians of ArrayList Item %.2f =  %.4f \n", x, Math.toRadians(x));
		}
	}
}
Finding Radians of ArrayList Item 3.14 =  0.0548 
Finding Radians of ArrayList Item 1.05 =  0.0183 
Finding Radians of ArrayList Item 0.79 =  0.0137 
Finding Radians of ArrayList Item 0.52 =  0.0091 
Finding Radians of ArrayList Item 0.35 =  0.0061 

在此 toRadians 函数示例中,我们声明了一个 double 类型的 ArrayList 并使用 Math.PI 常量(其值约为 3.14)分配了随机值。

ArrayList<Double> myList = new ArrayList<Double>(5);
myList.add(Math.PI);
myList.add(Math.PI / 3);
myList.add(Math.PI / 4);
myList.add(Math.PI / 6);
myList.add(Math.PI / 9);

接下来,我们使用Java For 循环来迭代 ArrayList 中的 double 值

for (double x : myList) {

这里,编译器将调用 math.toRadians 方法( static double toRadians(double x) )来查找相应的弧度值,并打印输出。

System.out.format("Finding Radians of ArrayList Item %.2f =  %.4f \n", x, Math.toRadians(x));