用java实现基础数学运算,函数等功能,包括:加减乘除阶乘,正弦余弦和指数运算
时间: 2024-03-09 08:46:04 浏览: 72
如何使用java来实现加减乘除运算.docx
好的,以下是一个简单的Java程序,演示了基本的数学运算、函数和阶乘:
```java
import java.util.Scanner;
public class BasicMathOperations {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入一个数字:");
double num1 = scanner.nextDouble();
System.out.print("请输入另一个数字:");
double num2 = scanner.nextDouble();
double sum = num1 + num2;
double difference = num1 - num2;
double product = num1 * num2;
double quotient = num1 / num2;
System.out.println("和为:" + sum);
System.out.println("差为:" + difference);
System.out.println("积为:" + product);
System.out.println("商为:" + quotient);
System.out.println("请输入一个角度(0-360度):");
double angle = scanner.nextDouble();
double radian = Math.toRadians(angle);
double sine = Math.sin(radian);
double cosine = Math.cos(radian);
double tangent = Math.tan(radian);
System.out.println("正弦值为:" + sine);
System.out.println("余弦值为:" + cosine);
System.out.println("正切值为:" + tangent);
System.out.print("请输入一个整数:");
int num = scanner.nextInt();
int factorial = 1;
for (int i = 1; i <= num; i++) {
factorial *= i;
}
System.out.println(num + "的阶乘为:" + factorial);
}
}
```
这个程序通过使用`Scanner`类从用户处获取输入,然后执行基本的数学运算和函数计算。其中,`Math`类提供了许多常见的数学函数,如`sine`、`cosine`和`tangent`等。阶乘的计算使用了一个简单的`for`循环。
阅读全文