我试图弄清楚如何设置此等式以为Java添加两个复数。
有两种方法,但是我不完全清楚第一种方法要我做什么。是说我需要做(real1 + imag1)而不是(real1 + real2)吗?如果是这种情况,那么我如何将结果输入c1?我也对第二种方法的结果难以持有并返回它。

    /*Method for adding the real and imaginary parts of two complex numbers,
 *which returns the result in a new complex number
 */
public static ComplexNumber addComplexNumbers(double real1, double imag1, double real2, double imag2){
    ComplexNumber result = ComplexNumber.addComplexNumbers(real1, imag1, real2, imag2);
    result.setReal(real1 + real2);
    result.setImag(imag1 + imag2);
    return result;
    }

//Method for adding two complex numbers
public static ComplexNumber addComplexNumbers(ComplexNumber c1, ComplexNumber c2){
    ComplexNumber result = new ComplexNumber();
    result = (c1.real + c2.real) + (c1.imag + c2.imag);

}

最佳答案

final public class Complex {

    private final double real;
    private final double imag;

    public Complex() {
        this(0.0, 0.0);
    }

    public Complex(double r) {
        this(r, 0.0);
    }

    public Complex(double r, double i) {
         this.real = r;
         this.imag = i;
    }

    public Complex add(Complex addend) {
        return new Complex((this.real + addend.real), (this.imag + addend.imag));
    }
}

09-05 11:22