java设计一个计算器
时间: 2024-05-04 16:12:47 浏览: 64
以下是一个简单的Java计算器的设计示例:
```java
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double num1, num2;
String operator;
System.out.print("请输入第一个数字:");
num1 = scanner.nextDouble();
System.out.print("请输入运算符(+、-、*、/):");
operator = scanner.next();
System.out.print("请输入第二个数字:");
num2 = scanner.nextDouble();
double result = 0;
switch (operator) {
case "+":
result = num1 + num2;
break;
case "-":
result = num1 - num2;
break;
case "*":
result = num1 * num2;
break;
case "/":
result = num1 / num2;
break;
default:
System.out.println("无效的运算符");
return;
}
System.out.println("计算结果:" + result);
}
}
```
阅读全文