1.问下这个关于复数的题哪里有问题?为什么运行不出来?
2.Java怎么通过scanner输入的参数个数来调用对应参数个数的构造方法的?
package step1;
import java.util.Scanner;
class Complex {
//定义属性
private double real;
private double imagin;
//定义构造方法
public Complex(){
}
public Complex(double real){
this.real = real;
}
public Complex(double real, double imagin){
this.real = real;
this.imagin = imagin;
}
//定义方法
public Complex Plus(Complex n3){
Complex n4 = new Complex();
n4.real = this.real + n3.real;
n4.imagin = this.imagin + n3.imagin;
return n4;
}
public Complex Sub(Complex n3){
Complex n4 = new Complex();
n4.real = this.real - n3.real;
n4.imagin = this.imagin - n3.imagin;
return n4;
}
//覆盖
@Override
public String toString(){
if(imagin>=0){
return(real + "+"+ imagin+ "i");
}else{
return(real + "-"+ imagin +"i");
}
}
}
class ComplexTest{
public static void main(String[] args){
Complex n0 = new Complex();
Scanner sc = new Scanner(System.in);
double real1 = sc.nextDouble();
double imagin1 = sc.nextDouble();
Complex n1 = new Complex(real1,imagin1);
//输出结果
System.out.println(n0.Plus(n1));
System.out.println(n0.Sub(n1));
System.out.print(n0 + " "+ n1);
}
}