一、结对小伙伴:陈淑筠 http://www.cnblogs.com/babybluecsj/
二、(1)我用小伙伴上一次5.1的作业来单元测试,发现当除数为零时,运行时未抛出异常,于是我们加上了
1. if(fh2=='/'){
if(b==0){ throw new Exception("除数不能为0!"); } answer=a/b; }2.
try {
res=Calculator.Onefhnum(1,1,3); } catch (Exception e) { e.printStackTrace(); Assert.fail("抛出异常,测试失败!"); }这段代码;
(2).还发现上次作业的代码有错误,三个运算符的最后的第一个为除数没有写上:
if(fh2=='/')//第一为除
{
if(fh3=='+')
{
if(fh4=='+')
{
if(b==0){
throw new Exception("除数不能为0!");
}
answer=a/b+c+d;
}
if(fh4=='-')
{
if(b==0){
throw new Exception("除数不能为0!");
}
answer=a/b+c-d;
}
if(fh4=='*')
{
if(b==0){
throw new Exception("除数不能为0!");
}
answer=a/b+c*d;
}
if(fh4=='/')
{
if(b==0){
throw new Exception("除数不能为0!");
}
if(d==0){
throw new Exception("除数不能为0!");
}
answer=a/b+c/d;
}
}
if(fh3=='-')
{
if(fh4=='+')
{
if(b==0){
throw new Exception("除数不能为0!");
}
answer=a/b-c+d;
}
if(fh4=='-')
{
if(b==0){
throw new Exception("除数不能为0!");
}
answer=a/b-c-d;
}
if(fh4=='*')
{
if(b==0){
throw new Exception("除数不能为0!");
}
answer=a/b-c*d;
}
if(fh4=='/')
{
if(b==0){
throw new Exception("除数不能为0!");
}
if(d==0){
throw new Exception("除数不能为0!");
}
answer=a/b-c/d;
}
}
if(fh3=='*')
{
if(fh4=='+')
{
if(b==0){
throw new Exception("除数不能为0!");
}
answer=a/b*c+d;
}
if(fh4=='-')
{
if(b==0){
throw new Exception("除数不能为0!");
}
answer=a/b*c-d;
}
if(fh4=='*')
{
if(b==0){
throw new Exception("除数不能为0!");
}
answer=a/b*c*d;
}
if(fh4=='/')
{
if(b==0){
throw new Exception("除数不能为0!");
}
if(d==0){
throw new Exception("除数不能为0!");
}
answer=a/b*c/d;
}
}
if(fh3=='/')
{
if(fh4=='+')
{
if(b==0){
throw new Exception("除数不能为0!");
}
if(c==0){
throw new Exception("除数不能为0!");
}
answer=a/b/c+d;
}
if(fh4=='-')
{
if(b==0){
throw new Exception("除数不能为0!");
}
if(c==0){
throw new Exception("除数不能为0!");
}
answer=a/b/c-d;
}
if(fh4=='*')
{
if(b==0){
throw new Exception("除数不能为0!");
}
if(c==0){
throw new Exception("除数不能为0!");
}
answer=a/b/c*d;
}
if(fh4=='/')
{
if(b==0){
throw new Exception("除数不能为0!");
}
if(c==0){
throw new Exception("除数不能为0!");
}
if(d==0){
throw new Exception("除数不能为0!");
}
answer=a/b/c/d;
}
}
}
return answer;
}
(3).在此,我们还增加了答案精确到了小数后一位:
static double answer=0.0d;
public static double Onefhnum(int a,int b,int e)throws Exception{//一个运算符的方法
public static double Twofhnum(int a,int b,int c,int e,int h)throws Exception{//两个运算符的方法
public static double Threefhnum(int a,int b,int c,int d,int e,int h,int f)throws Exception{//三个运算符的方法
(4).由于时间原因,在代码中增加了:int precision = 0;
if (NUM_MAX_OPERATOR == precision) { // 这里我可以处理最多三个运算符的字符串哦:) } else if (NUM_SCALE == precision) { // 这里我可以处理数值范围是 -100 到 100的字符串哦:) } else if (NUM_PRECISION == precision) { // 这里我可以处理精度是小数点后一位的字符串哦:) }这段代码后,还不知道如何增加和处理括号里的信息。