[code=Java][/code]
import java.io.*;
public class ComplexNumber{
public double real;
public double image;
public ComplexNumber(double a, double b)
{
this.real=a;
this.image=b;
}
public static String readString()
{
//从键盘读取字符串
BufferedReader br=new BufferedReader(new InputStreamReader(System.in),1);
String s1="";
try{
s1=br.readLine();
}catch(IOException ex){
System.out.println(ex);
return s1;
}
}
public static float readFloat(){
//字符串转换为浮点数
return Float.parseFloat(readString());
}
public ComplexNumber()
{
real=0.0;
image=0.0;
}
public ComplexNumber add(ComplexNumber c)
{
ComplexNumber result=new ComplexNumber(this.real+c.real,this.image+c.image);
return result;
}
public ComplexNumber sub(ComplexNumber c)
{
ComplexNumber result=new ComplexNumber(this.real-c.real,this.image-c.image);
return result;
}
public ComplexNumber mul(ComplexNumber c)
{
ComplexNumber result=new ComplexNumber(this.real*c.real-this.image*c.image,
this.real*c.image+this.image*c.real);
return result;
}
public ComplexNumber div(ComplexNumber c)
{
ComplexNumber result=new ComplexNumber((this.real*c.real+this.image*c.image)/(c.real*c.real+c.image*c.image),
(this.image*c.real+this.real*c.image)/(c.real*c.real+c.image*c.image));
return result;
}
public String toString(){
String ComplexNunber=new String(this.real+"+"+this.image+"i");
return ComplexNunber;
}
public static void main(String[] args )
{
ComplexNumber a=new ComplexNumber();
ComplexNumber b=new ComplexNumber();
System.out.println("Enter the first ComplexNumber");
a.real=ComplexNumber.readFloat();
a.image=ComplexNumber.readFloat();
System.out.println("Enter the second ComplexNumber");
b.real=ComplexNumber.readFloat();
b.image=ComplexNumber.readFloat();
System.out.println((a.add(b)).toString());
System.out.println((a.sub(b)).toString());
System.out.println((a.mul(b)).toString());
System.out.println((a.div(b)).toString());
}
}
------解决方案--------------------
把return s1;后面的 } 移动到return s1;的前面