|
|
|
@ -0,0 +1,55 @@
|
|
|
|
|
package ProjectStudy;
|
|
|
|
|
|
|
|
|
|
import java.util.Scanner;
|
|
|
|
|
|
|
|
|
|
public class SimpleCalculator {
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
Scanner scanner = new Scanner(System.in);
|
|
|
|
|
|
|
|
|
|
System.out.println("欢迎使用简单计算器!");
|
|
|
|
|
System.out.println("请输入第一个操作数:");
|
|
|
|
|
double num1 = scanner.nextDouble();
|
|
|
|
|
|
|
|
|
|
System.out.println("请输入操作符(+,-,*,/,%):");
|
|
|
|
|
char operator = scanner.next().charAt(0);
|
|
|
|
|
|
|
|
|
|
System.out.println("请输入第二个操作数:");
|
|
|
|
|
double num2 = scanner.nextDouble();
|
|
|
|
|
|
|
|
|
|
double result;
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
case '%':
|
|
|
|
|
if (num2 != 0 && Math.abs(num2) == (int) Math.abs(num2)) { // 检查第二个操作数是整数
|
|
|
|
|
result = (int) num1 % (int) num2;
|
|
|
|
|
} else {
|
|
|
|
|
System.out.println("错误:取余操作要求第二个操作数是整数!");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
System.out.println("错误:无效的操作符!");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
System.out.println("结果是:" + result);
|
|
|
|
|
}
|
|
|
|
|
}
|