我今天的目标是能够对复数(a + bi)进行加,减,乘和除运算,并以某种方式显示该数据。我知道当我尝试创建一种将复数相除的方法时会遇到困难。如果我的数学正确或((29-11i)/ 74),它应该显示大约0.39189-.1486i。我得到的输出是:-0.041666666666666664 + 0.4583333333333333i,这是不正确的。

您可以帮助我在ComplexDiv方法中找到错误吗?

这是代码:

public String ComplexDiv(ComplexNumbers other) {

        String r = Double.toString(((real*other.real)-(complex*other.complex))
                / ((other.real * other.real)-(other.complex * other.complex)));

        String c = Double.toString((-(real*other.complex)+(other.real*complex))
                / ((other.real * other.real)-(other.complex * other.complex)));

        return r + "+" + c + "i";
    }

这是调用该方法的测试类:
public class ComplexTest {
    public static void main(String[] args) {
        ComplexNumbers c1 = new ComplexNumbers();
        ComplexNumbers c2 = new ComplexNumbers();

        c1.real = 3;
        c1.complex = 2;
        c2.real = 5;
        c2.complex = 7;

        System.out.println(c1.ComplexAdd(c2));
        System.out.println(c1.ComplexSub(c2));
        System.out.println(c1.ComplexMult(c2));
        System.out.println(c1.ComplexDiv(c2));
        System.out.println(c1.findConjugate());
    }
}

奖励问题:知道如何用分数形式而不是十进制形式表示答案吗?

这是整个ComplexNumbers类,仅是为了概述我的方法:
package exerciseslearningjava;

public class ComplexNumbers {

    public double real;
    public double complex;
    public double realConvert;
    public double compConvert;


    public String ComplexAdd(ComplexNumbers other) {

        String r = Double.toString(real + other.real);
        String c = Double.toString(complex + other.complex);

        return r + "+" + c + "i";
    }
    public String ComplexSub(ComplexNumbers other) {

        String r = Double.toString(real - other.real);
        String c = Double.toString(complex - other.complex);

        return r + c + "i";
    }
    public String ComplexMult(ComplexNumbers other) {

        String r = Double.toString((real * other.real) - (complex*other.complex));
        String c = Double.toString((real * other.complex)+(complex*other.real));

        return r + "+" + c + "i";

    }
    public String ComplexDiv(ComplexNumbers other) {

        String r = Double.toString(((real*other.real)-(complex*other.complex))
                / ((other.real * other.real)-(other.complex * other.complex)));

        String c = Double.toString((-(real*other.complex)+(other.real*complex))
                / ((other.real * other.real)-(other.complex * other.complex)));

        return r + "+" + c + "i";
    }
    public String findConjugate() {
        String r = Double.toString(real);
        String c = Double.toString(complex);

        return r + "-" + c + "i";
    }
}

最佳答案

您的分母:

((other.real * other.real) - (other.complex * other.complex))

不应该是:
((other.real * other.real) + (other.complex * other.complex))

由于您是通过将复数乘以其共轭来获得的:
(a + bi) * (a - bi) = (a * a) - (b * b * i * i)

但是由于i * i为-1,因此变为:
(a + bi) * (a - bi) = (a * a) + (b * b)

另外,顺便说一句,当我看到字符串被用来表示数字值时,我的脖子后部会感到不适。为什么要使用字符串?为什么不浮动或BigDecimal?

10-07 19:08
查看更多