⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fushutestyichang.java

📁 复数四则运算以及异常处理 很简单
💻 JAVA
字号:
//FushuTestyichang.java
class LessParamException extends  Exception { 
private int inputNum; //用户输入的参数数目
public LessParamException(int a)  {inputNum=a;}
public int getInputNum( ) {return inputNum;}
public void handleException( ) {
System.out.println("需要输入5个参数,您只输入了"+getInputNum()+"个参数,程序中止。请再次运行程序时注意");
}
} 

class NoOpertaionException extends  Exception { //自定义的运算符异常
public NoOpertaionException( ){}
public void handleException( ){
System.out.println("缺乏操作符。请输入+、-、x、\\ 之一进行运算!");
}
}

class DividedByZeroException extends  Exception  {//自定义除数为零的异常
public DividedByZeroException( ){ }
public void handleException( ){
System.out.println("除数为零,请重新输入除数!");
}
}

class Fushu{ //复数类
	private float x,y;
	public float getShibu(float x){ return x;} //获得实部方法
	public float getXubu(float y){ return y;}//获得虚部方法
	    
   public String showFushu( ) { return x+"+"+y+"i"; }//显示复数
	      
   public Fushu add(Fushu f){//加法
		float a=this.x+f.x;
		float b=this.y+f.y;
		Fushu Result=new Fushu(a,b);
		return Result; }
	        
   public Fushu minus(Fushu f){//减法
		 float a=this.x-f.x;
		 float b=this.y-f.y;
		 Fushu Result=new Fushu(a,b);
		 return Result; }
	
   public Fushu mult(Fushu f){//乘法
      	 float a=this.x*f.x-this.y*f.y;
      	 float b=this.x*f.y+this.y*f.x;
      	 Fushu Result=new Fushu(a,b);
		 return Result; }
	    
 	public Fushu divd(Fushu f) throws  DividedByZeroException {//除法,声明抛出除数为0异常
		 if(f.x==0) throw new DividedByZeroException(); //若除数为0则抛出异常
        else {
float a=(this.x*f.x+this.y*f.y)/(f.x*f.x+f.y*f.y);
          float b=(this.y*f.x-this.x*f.y)/(f.x*f.x+f.y*f.y);
        	Fushu Result=new Fushu(a,b);
		    return Result;}
 	}
	public Fushu(float x,float y){//复数的构造方法
	    this.x=x;
	    this.y=y;}
}

public class FushuTestyichang {//主类
	public static void main(String[] args){
	  try{
		if (args.length<5) throw new LessParamException(args.length);//抛出缺少参数异常
	   //以下将args参数转换为float型后,构造出2个复数对象并显示:
Fushu f1=new Fushu(Float.parseFloat(args[0]), Float.parseFloat(args[1]));
    	Fushu f2=new Fushu (Float.parseFloat(args[2]), Float.parseFloat(args[3]
));
		System.out.println("您输入的复数是:");
		System.out.println("f1="+f1.showFushu());  //显示复数
       System.out.println("f2="+f2.showFushu());  //显示复数
	   //以下判断运算符
if (args[4].equals("+")) 
  {System.out.println("The sum="+f1.add(f2).showFushu());}   //f1+f2
		else if (args[4].equals("-")) 
{System.out.println("The differs="+f1.minus(f2).showFushu());}   //f1-f2
else if (args[4].equals("x"))  
{System.out.println("The product="+f1.mult(f2).showFushu());}   //f1xf2
		else if (args[4].equals("\\")) 
{System.out.println("The quotient="+f1.divd(f2).showFushu());}  //f1\f2
		else {throw new NoOpertaionException();}//抛出运算符错误的异常
 		}
	  catch(LessParamException e1)  {e1.handleException( );}
	  catch(DividedByZeroException e2) {e2.handleException( );}
	  catch(NoOpertaionException e3)  {e3.handleException( ); }
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -