MINAMI 2 months ago
parent c3b36a210a
commit 48c6e02a67

@ -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);
}
}

Loading…
Cancel
Save