求二次方程的解<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这是怎么回事啊?
<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这是怎么回事啊?
<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>