求大神解决问题!!复数类,其他都正常,一旦输入2+i 和 3-5i 结果就成了 2i ??
程序代码:
package com.ShiYan; import *; public class ShiYan03_01 { public static void main(String[] args) throws IOException { // TODO Auto-generated method stub Complex c, c1, c2; c = new Complex(0, 0); double Real[], Imginary[]; Real = new double[2]; Imginary = new double[2]; String str; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); for (int i = 0; i < 2; i++) { System.out.println("请分别输入第" + (i + 1) + "个数的实部和虚部:"); str = br.readLine(); Real[i] = Double.parseDouble(str); str = br.readLine(); Imginary[i] = Double.parseDouble(str); } c1 = new Complex(Real[0], Imginary[0]); c2 = new Complex(Real[1], Imginary[1]); int select; System.out.println("请选择想要进行的运算序号: 0.加 1.减 2.乘 3。除"); System.out.println("请输入想要进行的运算(请输入相应运算序号):"); str = br.readLine(); select = Integer.parseInt(str); if (select == 0) c = c.Add(c1, c2); else if (select == 1) c = c.Minus(c1, c2); else if (select == 2) c = c.Multiply(c1, c2); else if (select == 3) c = c.Divide(c1, c2); if (c.getImginaryPart() > 0) System.out.println(c.getRealPart() + "+" + c.getImginaryPart() + "i"); else System.out.println(c.getRealPart() + c.getImginaryPart() + "i"); } } class Complex { // 构造复数类 private double RealPart, ImginaryPart; public Complex(double r, double i) {// 构造函数 RealPart = r; ImginaryPart = i; } public Complex() {// 无参构造函数 RealPart = 0; ImginaryPart = 0; } public double getRealPart() { return RealPart; } public double getImginaryPart() { return ImginaryPart; } public Complex Add(Complex c1, Complex c2) { Complex sum = new Complex(); sum.RealPart = c1.RealPart + c2.RealPart; sum.ImginaryPart = c1.ImginaryPart + c2.ImginaryPart; return sum; } public Complex Minus(Complex c1, Complex c2) { Complex minus = new Complex(); minus.RealPart = c1.RealPart - c2.RealPart; minus.ImginaryPart = c1.ImginaryPart - c2.ImginaryPart; return minus; } public Complex Multiply(Complex c1, Complex c2) { Complex multiply = new Complex(); multiply.RealPart = c1.RealPart * c2.RealPart - c1.ImginaryPart * c2.ImginaryPart; multiply.ImginaryPart = c1.ImginaryPart * c2.RealPart + c1.RealPart * c2.ImginaryPart; return multiply; } public Complex Divide(Complex c1, Complex c2) { Complex divide = new Complex(); divide.RealPart = (c1.RealPart * c2.RealPart + c1.ImginaryPart * c2.ImginaryPart) / (c2.RealPart * c2.RealPart + c2.ImginaryPart * c2.ImginaryPart); divide.ImginaryPart = (c1.ImginaryPart * c2.RealPart - c1.RealPart * c2.ImginaryPart) / (c2.RealPart * c2.RealPart + c2.ImginaryPart * c2.ImginaryPart); return divide; } }