import java.util.Scanner;

public class Calculator {
public static void main(String[] args) {
while (true) {
Scanner scanner = new Scanner(System.in);
String str = scanner.nextLine();

if (str.equals("quit")) {
System.out.println("已退出计算器");
break;
} else {
switch (str) {
case "+":
System.out.println("请输入两个数,以回车分割");
Scanner addScanner1 = new Scanner(System.in);
double addNum1 = addScanner1.nextDouble();
Scanner addScanner2 = new Scanner(System.in);
double addNum2 = addScanner2.nextDouble();
double result1 = add(addNum1,addNum2);
System.out.println(addNum1 + "+" + addNum2 + "=" + result1);
break;
case "-":
System.out.println("请输入两个数,以回车分割");
Scanner subtractScanner1 = new Scanner(System.in);
double subtractNum1 = subtractScanner1.nextDouble();
Scanner subtractScanner2 = new Scanner(System.in);
double subtractNum2 = subtractScanner2.nextDouble();
double result2 = subtract(subtractNum1,subtractNum2);
System.out.println(subtractNum1 + "-" + subtractNum2 + "=" + result2);
break;
case "x":
System.out.println("请输入两个数,以回车分割");
Scanner multiplyScanner1 = new Scanner(System.in);
double multiplyNum1 = multiplyScanner1.nextDouble();
Scanner multiplyScanner2 = new Scanner(System.in);
double multiplyNum2 = multiplyScanner2.nextDouble();
double result3 = multiply(multiplyNum1,multiplyNum2);
System.out.println(multiplyNum1 + "x" + multiplyNum2 + "=" + result3);
break;
case "/":
System.out.println("请输入两个数,以回车分割");
Scanner divideScanner1 = new Scanner(System.in);
double divideNum1 = divideScanner1.nextDouble();
Scanner divideScanner2 = new Scanner(System.in);
double divideNum2 = divideScanner2.nextDouble();
double result4 = divide(divideNum1,divideNum2);
System.out.println(divideNum1 + "/" + divideNum2 + "=" + result4);
break;
case "阶乘":
System.out.println("请输入求阶乘数");
Scanner jxScanner = new Scanner(System.in);
int jxNum = jxScanner.nextInt();
int result = f(jxNum);
System.out.println(jxNum+"的阶乘是:"+result);
}
}
System.out.println();
System.out.println("请输入计算类型(+ - x / 阶乘)");
}

}

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

public static int f(int i) {
if (i == 1) {
return 1;
} else {
i = i * f(i - 1);
}
return i;
}
}

posted on 2021-12-11 12:44    阅读(66)  评论(0编辑  收藏  举报