输出的复数精度太高数字太长,我定义了double型数字格式化函数,将复数精确到小数点后2位,
可是输出时却抛出异常,提示为:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String ind
ex out of range: 4
        at java.lang.String.substring(String.java:1441)
        at Complex.Num_Format(Complex.java:25)
        at Complex.PrintComplex(Complex.java:30)
        at Complex.main(Complex.java:151)
       (好象是main函数调用PrintComplex出现异常)
Complex.java程序清单:
public class Complex
{
      private double real,image;
     
      Complex()
      {
          real=0;
          image=0;
      }
      Complex(double x,double y)
      {
      real=x;
      image=y;
      }
      void prt(String s)
      {
       System.out.print(s);
      }
      String Num_Format(double e)    //Double数字格式化函数(小数点后两位)
      {
       String str=new String(String.valueOf(e));  //将e转化为字符串,并赋给str
        int i=str.indexOf(46);   //str中小数点的位置(ASC 码为46)
25行:  String s=str.substring(0,i+3);
       return s;
      }
      void PrintComplex()
      { 
30行:    String Str_real=Num_Format(this.real),Str_image=Num_Format(this.image);
          if(image>0)       prt(Str_real+"+"+Str_image+"i   ");
          else if(image<0)  prt(Str_real+""+Str_image+"i   ");
          else if(image==0) prt(Str_real+"   ");
      }
      /*void PrintComplex()
      { 
          if(image>0)       prt(real+"+"+image+"i   ");
          else if(image<0)  prt(real+""+image+"i   ");
          else if(image==0) prt(real+"   ");
      }*/
      void Addition(Complex obj)    //复数加法
      {
       this.real=this.real+obj.real;
       this.image=this.image+obj.image;
      }
      void Subtration(Complex obj)  //减法
      {
       this.real=this.real-obj.real;
       this.image=this.image-obj.image;
      }
      
      /*static Complex Multiplication(Complex obj)
      {
       Complex temp=new Complex(1,0);
       temp.real=temp.real*obj.real-temp.image*obj.image;
       temp.image=temp.real*obj.image+temp.image*obj.real;
       return temp;
      }*/
      void Multiplication(Complex obj) //乘法
      {
       double i=this.real,j=this.image;
       this.real=i*obj.real - j*obj.image;
       this.image=i*obj.image + j*obj.real;
      }
      void Division(Complex obj)      //除法
      {
       double i=this.real,j=this.image;
       this.real =(i*obj.real+j*obj.image)/(Math.pow(obj.real,2)+Math.pow(obj.image,2));
       this.image=(j*obj.real-i*obj.image)/(obj.real*obj.real+obj.image*obj.image);
      }
      void First_SubDiv(Complex obj)  
      {
       this.real=obj.real;
       this.image=obj.image;
      }
      
      void Triangle()         //实现复数三角表示的函数
      {
 double r,t,x;
 r=Math.sqrt(Math.pow(real,2)+Math.pow(image,2));
 x=image/real;
 if(real<0) t=Math.atan(x)*180/3.14+180;
 else t=Math.atan(x)*180/3.14;
 if(t<0) prt(r+"(cos("+t+")+isin("+t+"))\n");
 else prt(r+"(cos"+t+"+isin"+t+")\n");
      }
      public static void main(String []args)
      {
          int max=Integer.parseInt(args[0]);
          
          int m1=0,m2=0,m3=0,n1=0,n2=0,n3=0;
          int sm=0,sn=0;
          
          Complex Sum=new Complex();      //和对象
          Complex Sub=new Complex();      //差
          Complex Mult=new Complex(1,0);  //积
          Complex Div=new Complex(1,0);   //商
          Complex c[]=new Complex[max];              
         
          System.out.println("请输入"+max+"个复数(最多3位数):");
          for(int i=0;i<max;++i)
          {
               System.out.println("第"+(i+1)+"个复数:");
               System.out.print("实部:");  //每输入一个字符串,要两次调用System.in.read(),以接收回车符             
               try{
                     m1=System.in.read()-48;          //将接收的数字字符转化为相应的数字,      
                     m2=System.in.read()-48;
                     m3=System.in.read()-48;          
                     if(m3!=-35) System.in.read();    //用于接收回车符
                     System.in.read();   //用于接收回车符
                     
                }catch(Exception e){ };
               if(m1==-3)     // m1为  "-" 负号
               {
                  if(m3==-35) sm=-m2;
                  else sm=-(m2*10+m3);
               }
               else
               {
                  if(m2==-35) sm=m1;                    //输入的第二个字符为 '\n'(ASC码为13) 
                  else if(m3==-35) sm=m1*10+m2;
                  else sm=m1*100+m2*10+m3;
               }
               System.out.print("虚部:");   
               try{
                     n1=System.in.read()-48;
                     n2=System.in.read()-48;
                     n3=System.in.read()-48;
                     if(n3!=-35) System.in.read();   //用于接收回车符
                     System.in.read();   //用于接收回车符
                }catch(Exception e){ };
                if(n1==-3)     // n1为  "-" 负号
                {
                 if(n3==-35) sn=-n2;
                 else sn=-(n2*10+n3);
                }
                else
                {
                    if(n2==-35) sn=n1;
                    else if(n3==-35) sn=n1*10+n2;
                    else sn=n1*100+n2*10+n3;
                }
                c[i]=new Complex(sm,sn);
          }
                                                                                                          
          for(int i=0;i<max;++i)
          {
151行:         c[i].PrintComplex();
          
           Sum.Addition(c[i]);
           if(i==0) Sub.First_SubDiv(c[i]);//Sub=c[i];
           else Sub.Subtration(c[i]);
           //if(i==0) Mult=c[i];
           //else Mult.Multiplication(c[i]);
           Mult.Multiplication(c[i]);
           if(i==0) Div.First_SubDiv(c[i]);
           else Div.Division(c[i]);
          }
          System.out.print("\n\n");
          //try{
           Sum.PrintComplex();
          //}catch(StringIndexOutOfBoundsException e){ };  
          Sum.Triangle();
          
          //try{
           Sub.PrintComplex();
          //}catch(StringIndexOutOfBoundsException e){ };
          Sub.Triangle();
          
          //try{
           Mult.PrintComplex();
          //}catch(StringIndexOutOfBoundsException e){ };
          Mult.Triangle();
           
          //try{
           Div.PrintComplex();
          //}catch(StringIndexOutOfBoundsException e){ }; 
          Div.Triangle();
      }
}而我用Double_String.java模拟了main函数调用 输出函数 的例子却一切正常
Double_String.java程序清单:public class Double_String
{
private double r;
Double_String(double x)
{
r=x;
}
String Num_Format(double e)    //Double数字格式化函数(小数点后两位)
    {
       String str=new String(String.valueOf(e));  //将e转化为字符串,并赋给str
        int i=str.indexOf(46);   //str中小数点的位置(ASC 码为46)
       String s=str.substring(0,i+3);
       return s;
    }
    void Print()
    {
     String str=Num_Format(this.r);
     System.out.println("保留小数点后两位:"+str);
    }
    public static void main(String []args)
{
System.out.println(String.valueOf(pi).substring(0,String.valueOf(pi).indexOf(46)+3));*/

//double e=Double.valueOf(args[0]).doubleValue();//将args[0]字符串转化为Double对象,
                                           //再用doubleValue()将Double对象转化为double值
double e=Double.parseDouble(args[0]);
Double_String obj=new Double_String(e);
obj.Print();
}
} 请高手指点一下怎么一回事,如何解决????????