利用JAVA语言设计一个简单的科学计算器,实现加减乘除及开根号、对数、平方、三角正/余弦、求倒数、进制转换的简单功能。
时间: 2023-07-10 13:19:38 浏览: 200
好的,以下是一个简单的Java科学计算器代码示例:
```java
import java.util.Scanner;
public class ScientificCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("欢迎使用科学计算器!");
while (true) {
System.out.println("请选择要进行的操作:");
System.out.println("1. 加法");
System.out.println("2. 减法");
System.out.println("3. 乘法");
System.out.println("4. 除法");
System.out.println("5. 开根号");
System.out.println("6. 对数");
System.out.println("7. 平方");
System.out.println("8. 三角正/余弦");
System.out.println("9. 求倒数");
System.out.println("10. 进制转换");
System.out.println("0. 退出");
int choice = scanner.nextInt();
if (choice == 0) {
System.out.println("感谢使用科学计算器!");
break;
}
switch (choice) {
case 1:
System.out.println("请输入第一个数:");
double num1 = scanner.nextDouble();
System.out.println("请输入第二个数:");
double num2 = scanner.nextDouble();
System.out.println(num1 + " + " + num2 + " = " + (num1 + num2));
break;
case 2:
System.out.println("请输入第一个数:");
num1 = scanner.nextDouble();
System.out.println("请输入第二个数:");
num2 = scanner.nextDouble();
System.out.println(num1 + " - " + num2 + " = " + (num1 - num2));
break;
case 3:
System.out.println("请输入第一个数:");
num1 = scanner.nextDouble();
System.out.println("请输入第二个数:");
num2 = scanner.nextDouble();
System.out.println(num1 + " * " + num2 + " = " + (num1 * num2));
break;
case 4:
System.out.println("请输入第一个数:");
num1 = scanner.nextDouble();
System.out.println("请输入第二个数:");
num2 = scanner.nextDouble();
if (num2 == 0) {
System.out.println("除数不能为0!");
} else {
System.out.println(num1 + " / " + num2 + " = " + (num1 / num2));
}
break;
case 5:
System.out.println("请输入一个数:");
num1 = scanner.nextDouble();
if (num1 < 0) {
System.out.println("不能对负数进行开根号运算!");
} else {
System.out.println("√" + num1 + " = " + Math.sqrt(num1));
}
break;
case 6:
System.out.println("请输入一个数:");
num1 = scanner.nextDouble();
if (num1 <= 0) {
System.out.println("对数必须大于0!");
} else {
System.out.println("log(" + num1 + ") = " + Math.log10(num1));
}
break;
case 7:
System.out.println("请输入一个数:");
num1 = scanner.nextDouble();
System.out.println(num1 + "的平方是:" + Math.pow(num1, 2));
break;
case 8:
System.out.println("请输入一个角度(0-360):");
double angle = scanner.nextDouble();
System.out.println("sin(" + angle + ") = " + Math.sin(Math.toRadians(angle)));
System.out.println("cos(" + angle + ") = " + Math.cos(Math.toRadians(angle)));
break;
case 9:
System.out.println("请输入一个非0数:");
num1 = scanner.nextDouble();
System.out.println("1/" + num1 + " = " + (1 / num1));
break;
case 10:
System.out.println("请输入一个十进制数:");
int dec = scanner.nextInt();
System.out.println("转换成二进制:" + Integer.toBinaryString(dec));
System.out.println("转换成八进制:" + Integer.toOctalString(dec));
System.out.println("转换成十六进制:" + Integer.toHexString(dec));
break;
default:
System.out.println("无效的操作!");
break;
}
}
}
}
```
以上代码通过Scanner类获取用户输入的数字和操作符,然后使用switch语句进行计算,并将结果输出到控制台。除了基本的加减乘除运算外,还包括开根号、对数、平方、三角正/余弦、求倒数、进制转换等功能。请注意,上述代码只是一个简单的示例,没有包含太多的错误处理和完整的界面设计。
阅读全文