java
public class Calculator {
public static double calculate(double num1, double num2, char operator) {
double result = 0.0;
switch (operator) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
if (num2 != 0) {
result = num1 / num2;
} else {
System.out.println("除数不能为0");
}
break;
default:
System.out.println("无效的操作符");
}
return result;
}
public static void main(String[] args) {
double num1 = 10.0;
double num2 = 5.0;
char operator = '+';
double result = calculate(num1, num2, operator);
System.out.println("结果:" + result);
}
}
顶一下
(0)
0%
踩一下
(0)
0%
- 相关评论
- 我要评论
-
上一篇:返回栏目
下一篇:计算器 java怎么实现加法