用Java写一个简单的加减乘除计算器
时间: 2023-10-16 15:09:18 浏览: 73
简单的加减乘除计算器
4星 · 用户满意度95%
好的,为您提供一个简单的Java实现的加减乘除计算器:
```
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入第一个数字:");
double num1 = scanner.nextDouble();
System.out.print("请输入操作符(+,-,*,/):");
char operator = scanner.next().charAt(0);
System.out.print("请输入第二个数字:");
double num2 = scanner.nextDouble();
double result = 0;
switch (operator) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
if (num2 == 0) {
System.out.println("除数不能为0!");
return;
}
result = num1 / num2;
break;
default:
System.out.println("请输入正确的操作符!");
return;
}
System.out.println(num1 + " " + operator + " " + num2 + " = " + result);
}
}
```
希望能够对您有所帮助!
阅读全文