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个复数对象并显示:
else
{ 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( ); }
}
}为什么运行之后没法输入参数直接显示参数输入不够的异常
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个复数对象并显示:
else
{ 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( ); }
}
}为什么运行之后没法输入参数直接显示参数输入不够的异常
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个复数对象并显示:
else
{ 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( ); }
}
}