import java.util.Scanner;
public class CalculateDemo {
public static void main(String[] args) {
System.out.println("欢迎使用计算器,请输入y开始:");
Scanner scanner = new Scanner(System.in);
String start = scanner.next();
while (start.equals("y")){
System.out.println("请你输入计算公式:");
System.out.println("请输入第一个运算数:");
double a = scanner.nextDouble();
System.out.println("请输入运算符号:");
String b = scanner.next();
System.out.println("请输入第二个运算数:");
double c = scanner.nextDouble();
CalculateDemo test = new CalculateDemo();
switch (b){
case "+":
System.out.println(test.testa(a,c));
break;
case "-":
System.out.println(test.testb(a,c));
break;
case "*":
System.out.println(test.testc(a,c));
break;
case "/":
System.out.println(test.testd(a,c));
break;
}
}
scanner.close();
}
// 计算方法:
public double testa(double a,double b){
return a+b;
}
public double testb(double a,double b){
return a-b;
}
public double testc(double a,double b){
return a*b;
}
public double testd(double a,double b){
return a/b;
}
}
结果:
欢迎使用计算器,请输入y开始:
y
请你输入计算公式:
请输入第一个运算数:
10
请输入运算符号:
/
请输入第二个运算数:
15
0.6666666666666666
请你输入计算公式:
请输入第一个运算数: