在java中显示复数

时间:2014-12-02 02:34:32

标签: java string methods double

我今天的目标是能够对复数(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";
    }
}

1 个答案:

答案 0 :(得分:1)

你的分母:

((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)

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