如何制作此代码以便只打印最终答案?

时间:2011-11-20 14:39:52

标签: java

我需要这个只显示最终答案,而不是代码第一部分的答案。这是因为它目前显示两个答案;一个来自第一部分,一个来自最后一部分,我需要的只是最后一部分的答案显示

public class Arbitrary extends ArithmeticExpression {
 Arbitrary(double value1, double value2, double value3, char operator1, char operator2){
this.value1 = value1;
this.value2 = value2;
this.value3 = value3;
this.operator1 = operator1;
this.operator2 = operator2;
}


public double evaluate(){
    System.out.println("Arbitrary Answer Is");

    if (operator1 == '+'){            // this if statement covers all the signs that may come up,if the first sign is a +, then the program will add the two numbers but if it isn't. The program will continue to go to else until it finds the correct sign or states the rror message.//
        Addition add = new Addition(value1,value2);
        result1 = add.evaluate();
    }
    else if (operator1 == '-'){
        Subtraction sub = new Subtraction(value1,value2);
        result1 = sub.evaluate();
    }
    else if (operator1 == '*'){
        Multiplication mult = new Multiplication(value1,value2);
        result1 = mult.evaluate();
    }
    else if (operator1 == '/'){
        Division div = new Division(value1,value2);
        result1 = div.evaluate();
    }
    else{
        System.out.println("Invalid Sum!");
    }


    if (operator2 == '+')

             {
        Addition plus = new Addition(result1,value3);
        result2 = plus.evaluate();
            }


    else if (operator2 == '-')
    {
        Subtraction take = new Subtraction(result1,value3);
        result2 = take.evaluate();
    }
    else if (operator2 == '*'){
        Multiplication times = new Multiplication(result1,value3);
        result2 = times.evaluate();
    }
    else if (operator2 == '/'){
        Division share = new Division(result1,value3);
        result2 = share.evaluate();
    }
    else{
        System.out.println("Incorrect expression!");
    }

    return result2;
}

public double display() {
      System.out.println("Abitrary Question Is");
    System.out.println("("+value1 + "+" + value2+")" + " * "+ "("+value3+")");
    return result2;

}

}

这部分是我的主要内容:

Arbitrary a1 = new Arbitrary(6, 9, 2, '+', '*');
           a1.display();
           a1.evaluate();
           System.out.println();

这是我得到的输出: Abitrary问题是 (6.0 + 9.0)*(2.0) 加法答案是 15.0 乘法答案是 30.0

我应该得到的是: Abitrary问题是 (6.0 + 9.0)*(2.0) 任意答案是 30.0

addition.java

   class Addition extends ArithmeticExpression{
Addition(double value1, double value2){
   result = value1 + value2;
   this.value1 = value1;
    this.value2 = value2;
}


public double display() {
    System.out.println("Addition Question Is ");
    System.out.println(value1 + " + "+ value2);
    return result;

}
public double evaluate(){
    System.out.println("Addition Answer Is");
    System.out.println(result);
return result;
}
}

multiplication.java

   class Multiplication extends ArithmeticExpression{
Multiplication(double value1, double value2){
    result = value1 * value2;
    this.value1 = value1;
    this.value2 = value2;
}



public double display() {
      System.out.println("Multiplication Question Is");
    System.out.println(value1 + " * "+ value2);
    return result;

}
public double evaluate(){
      System.out.println("Multiplication Answer Is");
    System.out.println(result);
    return result;
}


}

2 个答案:

答案 0 :(得分:1)

问题出在课程Addition& Multiplication 我假设您在每个内部打印结果(并在其他运算符内) 这导致程序打印你所做的第一个动作的答案(这是补充) 然后是第二个动作(即乘法)

在这两个课程中,你可以看到:

public double evaluate(){
    System.out.println("Addition Answer Is");
    System.out.println(result);
return result;
}

打印“Addition Answer Is”+结果。

只需删除(或注释掉)Addition&的评估函数中的前两行。 Multiplication

添加:

System.out.println("Addition Answer Is");
System.out.println(result);

用于乘法

   System.out.println("Multiplication Answer Is");
    System.out.println(result);

删除不必要的打印件后,将代码更改为:

Arbitrary a1 = new Arbitrary(6, 9, 2, '+', '*');
           a1.display();
           System.out.println(a1.evaluate());
           System.out.println();

答案 1 :(得分:0)

据我所知,您提供的代码根本不打印答案,更不用说打印两次了。但是,每次调用evaluate时都会打印"Arbitrary Answer Is",并且在递归调用它时会看起来很奇怪。

所以我要猜测 这就是你所说的。如果确实如此,则修复是在evaluate开头删除println,并将其放在调用evaluate的方法中...