复数与复数的运算问题

为什么最后输入是这个啊?
test.Complex@3567135c|7.810249675906654
复数的加法:test.Complex@327471b5
复数的减法:test.Complex@4157f54e
复数的乘法:test.Complex@90f6bfd
复数的除法:test.Complex@47f6473

原代码在这里
主类:

package test;
public class ComplexDome{
    public static void main(String[] args) {
    Complex c1=new Complex();
    Complex c2=new Complex();
    Complex c3,c4,c5,c6,z,z1;
    System.out.print("请输入第一个复数的实部:");
    c1.setReal();
    System.out.print("请输入第一个复数的虚部:");
    c1.setImage();
    c1=new Complex(c1.getReal(),c1.getImage());
    z=c1;
    z1=c2;
    System.out.print("第一个复数是:");
    System.out.println(c1.getString(z));
    System.out.print("请输入第二个复数的实部:");
    c2.setReal();
    System.out.print("请输入第二个复数的虚部:");
    c2.setImage();
    c2=new Complex(c2.getReal(),c2.getImage());
    System.out.print("第二个复数是:");
    System.out.println(c2.getString(z1));
    System.out.println("|"+c1+"|"+c1.mod(z));
    System.out.print("复数的加法:");
    c3=c1.add(z);
    System.out.println(c3);
    System.out.print("复数的减法:");
    c4=c1.sub(c2);
    System.out.println(c4);
    System.out.print("复数的乘法:");
    c5=c1.multi(c2);
    System.out.println(c5);
    System.out.print("复数的除法:");
    c6=c1.div(c2);
    System.out.println(c6);
    }
}

package test;
import java.util.Scanner;
public class Complex{
Scanner input=new Scanner(System.in);
    private double real;
    private double image;
    String s;
    Complex z;
    Complex(double a,double b){
        real=a;
        image=b;
    }
    Complex(double a){
        real=a;
        image=0.0;
    }
    Complex(){
        real=0.0;
        image=0.0;
    }
    Complex(Complex c1){
        z=c1;
    }
    public void setReal(){
        real=input.nextDouble();
    }
    public void setImage(){
        image=input.nextDouble();
    }
    public double getReal(){
        return real;
    }
    public double getImage(){
        return image;
    }
    double mod(Complex y){
        return Math.sqrt(y.real*y.real+y.image*y.image);
    }
    Complex add(Complex y){
        return new Complex(real+y.real,image+y.image);
    }
    Complex sub(Complex y){
        return new Complex(real-y.real,image+y.image);
    }
    Complex multi(Complex y){
        return new Complex(real*y.getReal()-image*y.getImage(),real*y.getImage()+image*y.getReal());
    }
    Complex div(Complex y)
    {
    double real=(this.real*y.getReal()+this.image*y.getImage())/(y.getReal()*y.getReal()+y.getImage()*y.getImage());
    double image=(this.image*y.getReal()-y.real*y.getImage())/(y.getReal()*y.getReal()+y.getImage()*y.getImage());
    return new Complex(real,image);
    }
    public String getString(Complex z){
            if(z.getReal()==0&&z.getImage()==0)
                return "0"; 
            else if(z.getReal()==0&&z.getImage()!=0)
                return ""+z.getImage()+"i";
            else if(z.getReal()!=0&&z.getImage()==0)
                return ""+z.getReal();
            else {
                if(image<0){
                return ""+z.getReal()+z.getImage()+"i";
                }
                else{
                return ""+z.getReal()+"+"+z.getImage()+"i";
                }
            }
    }
}





重写tostring方法

加上一个

@override
public String toString()
{
    return getString(this);
}

  • 这篇博客: Java学习笔记(五):Complex类的设计及加减乘除运算的实现中的 四、带有test的源代码 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  •  package stu.crayue.complex;
    /**
    * @author Crayue
    * @version 2019年10月24日 上午10:15:54
    */
    public class Complex {
    	private double real;
    	private double vir;
    	
    	public Complex() {//无参构造
    		this(0.0,0.0);
    	}
    
    	public Complex(double real) {//单参构造
    		this.real = real;
    	}
    	
    	public Complex(double real, double vir) {//双参构造
    		this.real = real;
    		this.vir = vir;
    	}
    	
    	public Complex(Complex c) {//Complex类型的单参构造
    		this(c.real,c.vir);
    	}
    
    	public double getReal() {
    		return real;
    	}
    
    	public void setReal(double real) {
    		this.real = real;
    	}
    
    	public double getVir() {
    		return vir;
    	}
    
    	public void setVir(double vir) {
    		this.vir = vir;
    	}
    	
    	@Override
    	public boolean equals(Object obj) {//覆盖object的equals方法
    		if(obj==null) {
    			return false;
    	}
    		if(this==obj) {
    			return true;
    		}
    		if(obj.getClass()!=this.getClass()) {
    			return false;
    		}
    		Complex c= (Complex) obj;//强转obj类型为Complex
    		
    		return Math.abs(this.real-c.real)<1e-6 && Math.abs(this.vir-c.vir)<1e-6;
    	}
    
    	@Override
    	public String toString() {//覆盖object中的toString方法
    		return "(" + real + "," +vir +")";
    	}
    	
    //加法
    	public Complex Add (Complex other) {
    		this.real+=other.real;
    		this.vir+=other.vir;
    		return this;
    	}
    	public static Complex Add(Complex one, Complex other) {
    		return new Complex(one).Add(other);
    	}
    	
    	//取相反数
    	private static Complex Opposite(Complex c) {
    		return new Complex(-c.real, -c.real);
    }
    	//减法
    	public Complex Sub(Complex other) {
    		return this.Add(Opposite(other));
    	}
    	
    	public static Complex Sub(Complex one, Complex other) {
    		  return new Complex(one).Sub(other);
    		//return new Complex(one).Add(Opposite(other));
    	}
    	
    	//乘法
    	public Complex Mul(Complex other) {
     		//必须realSave,防止值更改
    		double Savereal = this.real;
    		this.real = this.real * other.real - this.vir * other.vir;  
    		this.vir = Savereal * other.vir + this.vir * other.real;
    		return this;
    	}
    
    	public static Complex Mul(Complex one, Complex other) {
    		return new Complex(one).Mul(other);
    	}
    	
    	//取倒数
    	private static Complex Reciprocal(Complex c) {
    		double cc = c.real * c.real + c.vir * c.vir;
    		//分母为零 代码优化 使用三目运算符:
    		return (Math.abs(cc) < 1e-6)?null:new Complex(c.real /cc, -c.vir /cc) ;
    		/*if (Math.abs(cc) < 1e-6) {
    			return null;
    		}
    		return new Complex(c.real /cc, -c.vir /cc);	
    	}*/
    
    	public Complex Div(Complex other) {
    		Complex another = Complex.Reciprocal(other);
    		return another==null ? null :Mul(another);
    	}
    	
    	public static Complex Div (Complex c1,Complex c2) {
    		return new Complex(c1).Div(c2);
    		//Complex another=Reciprocal(c2);
    		//return another ==null?null:new Complex(c1).Mul(another);
    	}
    }
    

    test:

    package stu.crayue.complex.test;
    
    import stu.crayue.complex.Complex;
    
    /**
    * @author Crayue
    * @version 2019年10月24日 上午11:06:22
    */
    public class Test {
    
    	public static void main(String[] args) {
    		Complex c1= new Complex(0, 1.0);
    		Complex c2= new Complex(3.2, 5.3);
    		Complex c3= new Complex(1, 1.0);
    		
    		System.out.println(c2.Add(c1).Add(c3));
    		System.out.println(Complex.Add(c1, c2));//静态方法的调用
    	    System.out.println(Complex.Add(new Complex(0,1.0),new Complex(3.3,5.3)));//直接传入两个参数
    	    
    	    System.out.println(c2.Sub(c1));
    		System.out.println(c3.Mul(c1));
    		System.out.println(c3.Div(c1));
    	}
    }