import java.text.DecimalFormat;
import java.util.Scanner;
public class App {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
DecimalFormat df = new DecimalFormat("0.##");
String continueCalculation = "Y";
// 循环让用户进行计算
while ("Y".equalsIgnoreCase(continueCalculation)) {
System.out.println("请输入要执行的操作(加法、减法、乘法、除法):");
String operation = scanner.nextLine();
System.out.println("请输入第一个数字:");
double num1 = scanner.nextDouble();
System.out.println("请输入第二个数字:");
double num2 = scanner.nextDouble();
scanner.nextLine();
// 根据操作类型进行计算
switch (operation) {
case "加法":
System.out.println("结果是:" + df.format(add(num1, num2)));
break;
case "减法":
System.out.println("结果是:" + df.format(subtract(num1, num2)));
break;
case "乘法":
System.out.println("结果是:" + df.format(multiply(num1, num2)));
break;
case "除法":
if (num2 != 0) {
System.out.println("结果是:" + df.format(divide(num1, num2)));
} else {
System.out.println("除数不能为0");
}
break;
default:
System.out.println("未知的操作");
}
// 询问用户是否继续计算
System.out.println("是否继续计算?(Y/N)");
continueCalculation = scanner.nextLine();
}
scanner.close();
}
public static double add(double num1, double num2) {
return num1 + num2;
}
public static double subtract(double num1, double num2) {
return num1 - num2;
}
public static double multiply(double num1, double num2) {
return num1 * num2;
}
public static double divide(double num1, double num2) {
return num1 / num2;
}
}