<%@ page contentType= "text/html; charset=gb2312 " language= "java " import= "java.util.* " %>
<html>
<head>
<title> 计算三角形的面积 </title>
</head>
<body>
<p> 请输入三角形的三个边的长度: <br>
<form name= "form " method= "post " action= "triangle.jsp ">
<p>
<input name= "num1 " type= "text " >
</p>
<p>
<input name= "nume2 " type= "text ">
</p>
<p>
<input name= "nume3 " type= "text ">
</p>
<p>
<input type= "submit " value= "计算 ">
</p>
</form>
<%! double a ,b,c;
String strA,strB,strC;
String answer;
double result;
%>
<% if(request.getParameter( "submit ")!=null)
{
strA=request.getParameter( "num1 ");
strB=request.getParameter( "num2 ");
strC=request.getParameter( "num3 ");
try
{
a=Double.valueOf(strA);
b=Double.valueOf(strB);
c=Double.valueOf(strC);
}
catch(NumberFormatException exc)
{
System.out.println( " <br> "+ "请输入数字字符 ");
}
if((a+b)> c&&(a+c)> b&&(b+c)> a)
{
double p=(a+b+c)/2;
result=Math.sqrt(p*(p-a)*(p-b)*(p-c));
System.out.println( "面积: "+result);
}
else
{
answer= "您输入的三边不能构成一个三角形 ";
System.out.print( " <br> "+answer);
}
}
%>
</body>
</html>
执行后:当输入a b c的值为:3 4 5时,不输出结果!
输入其它不合法的字符时.也不提示错误信息!!
------解决方案--------------------
if(request.getParameter( "submit ")!=null)
你是想判断是否是通过submit按钮提交过来的
但是注意你的 <input type= "submit " value= "计算 ">
没有name
应该是 <input type= "submit " name= "submit " value= "计算 ">
------解决方案--------------------
改成这样 ~~
<form name= "form " method= "post " action= "./test.jsp ">
<p>
<input name= "num1 " type= "text ">
</p>
<p>
<input name= "num2 " type= "text ">
</p>
<p>
<input name= "num3 " type= "text ">
</p>
<p>
<input type= "submit " name= "submit " value= "计算 ">
</p>
</form>
错误原因:
1,num2,num3名称前后不一致;
2,submit没给名称
------解决方案--------------------
另外,System.out.print会输出到控制台,如果想输出到页面要使用out.println()
------解决方案--------------------
楼上正解