拿去试试
[Python] 纯文本查看 复制代码 import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
double num1;
double num2;
char operator;
double result;
Scanner scanner = new Scanner(System.in);
System.out.println("请输入第一个数字:");
num1 = scanner.nextDouble();
System.out.println("请输入第二个数字:");
num2 = scanner.nextDouble();
System.out.println("请输入运算符(+, -, *, /):");
operator = scanner.next().charAt(0);
scanner.close();
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("除数不能为零!");
return;
}
break;
default:
System.out.println("无效的运算符!");
return;
}
System.out.println(String.format("%.2f %c %.2f = %.2f", num1, operator, num2, result));
}
}
|