Java 高级计算器代码
时间: 2023-07-09 10:35:58 浏览: 96
Advance-Gui-Calulator:这是Java中的高级计算器
以下是一个简单的 Java 高级计算器代码,可以进行基本的数学运算和三角函数运算:
```java
import java.util.Scanner;
import java.lang.Math;
public class Calculator {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double num1, num2, result;
char operator;
System.out.print("Enter first number: ");
num1 = input.nextDouble();
System.out.print("Enter operator (+, -, *, /, s, c, t): ");
operator = input.next().charAt(0);
if (operator == 's' || operator == 'c' || operator == 't') {
num1 = Math.toRadians(num1);
}
if (operator == '+' || operator == '-' || operator == '*' || operator == '/') {
System.out.print("Enter second number: ");
num2 = input.nextDouble();
switch (operator) {
case '+':
result = num1 + num2;
System.out.println(num1 + " + " + num2 + " = " + result);
break;
case '-':
result = num1 - num2;
System.out.println(num1 + " - " + num2 + " = " + result);
break;
case '*':
result = num1 * num2;
System.out.println(num1 + " * " + num2 + " = " + result);
break;
case '/':
result = num1 / num2;
System.out.println(num1 + " / " + num2 + " = " + result);
break;
}
} else {
switch (operator) {
case 's':
result = Math.sin(num1);
System.out.println("sin(" + num1 + ") = " + result);
break;
case 'c':
result = Math.cos(num1);
System.out.println("cos(" + num1 + ") = " + result);
break;
case 't':
result = Math.tan(num1);
System.out.println("tan(" + num1 + ") = " + result);
break;
}
}
}
}
```
这个计算器可以进行基本的加、减、乘、除运算,以及三角函数的计算(输入 s、c、t 分别表示求正弦、余弦、正切)。需要注意的是,输入角度时需要将角度转换为弧度进行计算。
阅读全文