求二次方程的解<body>
<form action="" method="get" name="form">
   <input type="text" name="v1" value="0" >
        X<sup>2</sup>+
   <input type="text" name="v2" value="0" >
        X+
   <input type="text" name="v3" value="0" >
        =0
   <input type="submit" value="计算" name="submit" >
</form>
<%!
   public class A
   {
       double a,b,c;
       A(double a,double b,double c)
       {
           this.a=a;
           this.b=b;
           this.c=c;
       }
       double seek()
       {
           double x1,x2;
           if(b*b-4*a*c<0)
           {          System.out.println("无解。");           }
           
   if(b*b-4*a*c==0)
           {
               x1=(Math.sqrt(b*b-4*a*c)-b)/2*a;
               x2=(Math.sqrt(b*b-4*a*c)-b)/2*a;               System.out.println("两个相同根:x1="+x1+",x2="+x2);           }
           
   if(b*b-4*a*c>0)
           {
               x1=(Math.sqrt(b*b-4*a*c)-b)/2*a;
               x2=(Math.sqrt(b*b-4*a*c)-b)/2*a;               System.out.println("两个不同根:x1="+x1+",x2="+x2);           }
       }
   }
%>
<%
   String str1=request.getParameter("v1");
   String str2=request.getParameter("v2");
   String str3=request.getParameter("v3");
   double a,b,c;
   
   if(str1!=null && str2!=null && str3!=null)
   {
       a=Double.parseDouble(str1);
       b=Double.parseDouble(str2);
       c=Double.parseDouble(str3);
   }
   else
   {
       a=0;
       b=0;
       c=0;
   }
   
   A x=new A(a,b,c);
   x.seek();%>
</body>
Tomcat报错为:
Generated servlet error:
This method must return a result of type double这是怎么回事啊?

解决方案 »

  1.   

    你的seek()方法定义是返回double,但你没有返回值,把x1和x2返回出来
      

  2.   

    seek()前的double 改为:void就OK了
      

  3.   

    This method must return a result of type double这个方法必须返回一个double类型的结果
      

  4.   

    是的,改成void就可以了。编译通过,但是还有个问题就是,计算不出来。不知道怎么回事。
      

  5.   

    你要输出到JSP页面上?那怎么会使用System.out.println()呢?
      

  6.   

    <body>
    <form action="" method="get" name="form">
       <input type="text" name="v1" value="0" >
            X<sup>2</sup>+
       <input type="text" name="v2" value="0" >
            X+
       <input type="text" name="v3" value="0" >
            =0
       <input type="submit" value="计算" name="submit" >
    </form>
    <%!
       public class A
       {
           double a,b,c;
           double x1,x2;
           A(double a,double b,double c)
           {
               this.a=a;
               this.b=b;
               this.c=c;
           }
           double seek()
           {
               
               if(b*b-4*a*c<0)
               {                 System.out.println("无解。");           }
               
               if(b*b-4*a*c==0)
               {
                   x1=(Math.sqrt(b*b-4*a*c)-b)/2*a;
                   x2=(Math.sqrt(b*b-4*a*c)-b)/2*a;               System.out.println("两个相同根:x1="+x1+",x2="+x2);           }
               
               if(b*b-4*a*c>0)
               {
                   x1=(Math.sqrt(b*b-4*a*c)-b)/2*a;
                   x2=(Math.sqrt(b*b-4*a*c)-b)/2*a;               System.out.println("两个不同根:x1="+x1+",x2="+x2);           }
           }
       }
    %>
    <%
       String str1=request.getParameter("v1");
       String str2=request.getParameter("v2");
       String str3=request.getParameter("v3");
       double a,b,c;
       
       if(str1!=null && str2!=null && str3!=null)
       {
           a=Double.parseDouble(str1);
           b=Double.parseDouble(str2);
           c=Double.parseDouble(str3);
       }
       else
       {
           a=0;
           b=0;
           c=0;
       }
       
       A x=new A(a,b,c);
       x.seek();
       out.println(x.x1);
       out.println(x.x2);%>
    </body>