master
dsc 2 months ago
parent 488edb056f
commit 9aaa5210e3

@ -1,2 +1,63 @@
import java.util.Scanner;
public class Computer { public class Computer {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入第一个数字:");
double num1 = scanner.nextDouble();
System.out.println("请输入第二个数字:");
double num2 = scanner.nextDouble();
System.out.println("请选择操作 (+, -, *, /):");
String operation = scanner.next();
double result;
switch (operation) {
case "+":
result = add(num1, num2);
break;
case "-":
result = subtract(num1, num2);
break;
case "*":
result = multiply(num1, num2);
break;
case "/":
if (num2 != 0) {
result = divide(num1, num2);
} else {
System.out.println("除数不能为零!");
return;
} }
break;
default:
System.out.println("无效的操作符!");
return;
}
System.out.printf("%.2f %s %.2f = %.2f\n", num1, operation, num2, result);
}
// 加法
public static double add(double a, double b) {
return a + b;
}
// 减法
public static double subtract(double a, double b) {
return a - b;
}
// 乘法
public static double multiply(double a, double b) {
return a * b;
}
// 除法
public static double divide(double a, double b) {
return a / b;
}
}

Loading…
Cancel
Save