需要帮助将问题与答案合并

时间:2011-11-18 11:22:01

标签: java

我正在创建一个计算器,它必须计算出像(12.0 + 10.0)* 3.0这样的表达式 到目前为止,我已根据每个数学运算创建了子类。

例如:

的增添。的java

class Addition extends ArithmeticExpression {
    Addition(double value1, double value2) {
        result = value1 + value2;
        this.value1 = value1;
        this.value2 = value2;
    }
    public Addition( ArithmeticExpression an,double num2) {
        value1 = an.evaluate();
        value2 = num2;
        result1 = value1 + value2;
    }
    public double evaluate1() {
        System.out.println("Arbitrary Answer Is");
        return result1;
    }
    public double display1() {
        System.out.println("Arbitrary Question Is");
        System.out.println( "(" + value1 + "+"  + value2 + ")" + " =" + result1);
        return result1;
    }
    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;
    }
}

和另一个相似的乘法,但是相乘而不是增加。 然后我创建了一个类,我正在回答这个课程的问题如下。 Arbitrary.java

public class arbitrary {
    public Arbitrary() {
           Addition arbitrary1 = new Addition(12.0,10.0);
           arbitrary1.display1();
           Multiplication arbitrary2 = new Multiplication(arbitrary1,3.0);
           arbitrary2.display2();
    }
}

当它显示答案时显示:

Arbitrary Question Is
(12.0+10.0) =0.0
Addition Answer Is
22.0
Arbitrary Question Is
(22.0*3.0) = 66.0

关于我如何在答案旁边打印原始问题的任何建议?

1 个答案:

答案 0 :(得分:2)

您可以创建一个返回添加内容的方法,如

   long val=arbitrary1.display1(); 

将display1()的值存储在变量中,并传递

中变量的值
    Multiplication arbitrary2 = new Multiplication(val,3.0);