帮我用java写一个简易科学计算器
时间: 2023-08-14 14:04:34 浏览: 89
好的,下面是一个简易的科学计算器的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.print("请输入要进行的操作:\n1. 加法\n2. 减法\n3. 乘法\n4. 除法\n5. 指数\n6. 平方根\n7. 退出\n");
int choice = scanner.nextInt();
if (choice == 7) {
System.out.println("感谢使用科学计算器!");
break;
}
double result = 0.0;
switch (choice) {
case 1:
System.out.print("请输入两个数:");
double num1 = scanner.nextDouble();
double num2 = scanner.nextDouble();
result = num1 + num2;
break;
case 2:
System.out.print("请输入两个数:");
num1 = scanner.nextDouble();
num2 = scanner.nextDouble();
result = num1 - num2;
break;
case 3:
System.out.print("请输入两个数:");
num1 = scanner.nextDouble();
num2 = scanner.nextDouble();
result = num1 * num2;
break;
case 4:
System.out.print("请输入两个数:");
num1 = scanner.nextDouble();
num2 = scanner.nextDouble();
result = num1 / num2;
break;
case 5:
System.out.print("请输入一个数和指数:");
num1 = scanner.nextDouble();
num2 = scanner.nextDouble();
result = Math.pow(num1, num2);
break;
case 6:
System.out.print("请输入一个数:");
num1 = scanner.nextDouble();
result = Math.sqrt(num1);
break;
default:
System.out.println("无效的选择!");
continue;
}
System.out.println("结果是:" + result);
}
}
}
```
这个计算器可以进行加、减、乘、除、指数和平方根等操作。你可以根据自己的需求进行修改和扩展。
阅读全文