当前位置: 代码迷 >> J2SE >> java 复数类。定义的四个 方法 声明无效需要返回类型。请各位大侠,前辈帮忙看看解决方案
  详细解决方案

java 复数类。定义的四个 方法 声明无效需要返回类型。请各位大侠,前辈帮忙看看解决方案

热度:585   发布时间:2016-04-24 01:27:28.0
java 复数类。定义的四个 方法 声明无效需要返回类型。请各位大侠,前辈帮忙看看
import java.util.Scanner;
import java.io.*;
public class ComplexNumber{
private float real;

  private float image;

  public ComplexNumber(float a, float 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 add(ComplexNumber c)
  {
  ComplexNumber result=new ComplexNumber(this.real+c.real,this.image+c.image);

  return result;
  }
  public sub(ComplexNumber c)
  {
  ComplexNumber result=new ComplexNumber(this.real-c.real,this.image-c.image);
  return result;
  }
  public 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 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.imaginary+"i");

  return ComplexNunber;

  }
  }
  
public class 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());
  }
 
 

------解决方案--------------------
public sub(ComplexNumber c) public ComplexNumber sub(...)
  相关解决方案