2 u013251453 u013251453 于 2013.12.23 22:39 提问

java语言想要运算器程序可以运行出正负数及小数点运算

public class Calculator
{ private CalcEngine engine;
private UserGUI gui;
public Calculator()
{ engine=new CalcEngine();
gui=new UserGUI(engine); }

public void show()
{ gui.setVisible(true); }
public static void main(String a[])
{ Calculator calc=new Calculator();
calc.show(); }
}
class CalcEngine
{ private double number;double result;
final double pi=3.1415926535;
char flag;
boolean isNumber;
public CalcEngine()
{ number=0;
result=0;
flag=' ';
isNumber=true;
}
public double getDisplayValue()
{ return number; }
public void numberPressed(double number)
{ if(isNumber&&this.number!=0)
{ double str1=Double.toDouble(number);
double str2=Double.toDouble(this.number);
this.number=Double.parseDouble(str2+str1);
}
else
{ this.number=number; }
isNumber=true;
}
public void plus()
{ equals();
this.flag='+'; }
public void minus()
{ equals();
this.flag='-'; }
public void mul()
{ equals();
this.flag='*'; }
public void div()
{ equals();
this.flag='/'; }
public void genhao()
{ equals();
this.flag='√'; }
public void quyu()
{ equals();
this.flag='%'; }
public void sinzhi()
{ equals();
this.flag='s'; }
public void dian()
{ equals();
this.flag='.'; }
public double equals()
{ switch(flag)
{ case '+': result=result+number; break;
case '-': result=result-number; break;
case '*': result=result*number; break;
case '/': result=result/number; break;
case '√': result=Math.sqrt( number); break;
case '%': result=result%number; break;
case 's': result= Math.sin(number*pi/180);
break;
case '.': number=(double) number; break; //这里有错
case ' ': result= number; break;
}
return result;
flag=' ';
isNumber=false;
}
public void clear()
{ number=0; result=0; }
public String getTitle()
{ return " calclation example"; }
}

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!