|
|
|
@ -13,5 +13,36 @@ public class calculator {
|
|
|
|
|
System.out.print("请选择操作符 (+, -, *, /, %):");
|
|
|
|
|
String operation = scanner.next();
|
|
|
|
|
|
|
|
|
|
double result;
|
|
|
|
|
switch (operation) {
|
|
|
|
|
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;
|
|
|
|
|
case "%":
|
|
|
|
|
if (num2 == 0) {
|
|
|
|
|
System.out.println("错误:除数不能为0");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
result = num1 % num2;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
System.out.println("无效的操作符");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
System.out.printf("结果是: %.2f\n", result);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|