用java编写加减乘除计算器

159 2024-03-04 14:46

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%
相关评论
我要评论
点击我更换图片