import java.util.Scanner;
/*
狂神说:写一个计算器,要求实现加减乘除功能,并且能够循环接收新的数据,通过用户交互实现。思路推荐:
1. 写4个方法:加减乘除 2. 利用循环+switch进行用户交互 3. 传递需要操作的两个数 4. 输出结果
*/
public class Calc {
public double add(double a,double b){
return a+b;
}
public double red(double a,double b){
return a-b;
}
public double ride(double a,double b){
return a*b;
}
public double except(double a,double b){
return a/b;
}
public static void main(String[] args) {
Calc calc = new Calc();
while(true){
System.out.println("请输入+ - * /或quit退去计算器");
Scanner scanner = new Scanner(System.in);
String s = scanner.nextLine();
if(s.equals("quit")){
System.out.println("感谢使用");
break;
}
System.out.println("请输入两个数");
Scanner scan1 = new Scanner(System.in);
double a = scan1.nextDouble();
Scanner scan2 = new Scanner(System.in);
double b = scan2.nextDouble();
switch (s){
case "+":
System.out.println(a+s+b+"="+calc.add(a,b));
break;
case "-":
System.out.println(a+s+b+"="+calc.red(a,b));
break;
case "*":
System.out.println(a+s+b+"="+calc.ride(a,b));
break;
case "/":
System.out.println(a+s+b+"="+calc.except(a,b));
break;
default:
System.out.println("请重新输入");
break;
}
}
}
}
|