求java程序
求java编写的计算器程序,只要求加减乘除,
------解决方案--------------------http://download.csdn.net/detail/peng_hao1988/1189218
------解决方案--------------------public abstract class YunSuan {
private int a;
private int b;
public abstract int yun();
//默认构造 带参构造 get set封装
}
//加减乘除都继承YunSuan 注意除法做下判断 除数不为零
public class jia extends YunSuan {
@Override
public int yun() {
return this.getA()+this.getB();
}
public jia() {
super();
// TODO Auto-generated constructor stub
}
public jia(int a, int b) {
super(a, b);
// TODO Auto-generated constructor stub
}
//工厂类
public class Factory {
public static YunSuan ys(int a,int b,char op){
YunSuan oper=null;
switch(op){
case '+':oper=new jia(a,b);break;
case '-':oper=new jian(a,b);break;
case '*':oper=new cheng(a,b);break;
case '/':oper=new chu(a, b);break;
}
return oper;
}
}
//测试类
public class test {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.println("请输入一个数");
int a=in.nextInt();
System.out.println("请输入一个数");
int b=in.nextInt();
System.out.println("+-*/");
char op=in.next().charAt(0);
Factory factory=new Factory();
YunSuan oper=factory.ys(a, b, op);
//格式化输出
System.out.printf("%d%s%d=%d",a,op,b,oper.yun());
}
}