计算三角形的面积与周长
用户输入三个自然数:
<body>
<form action="verify1" method="post">
请输入三角形的三条边:<br/><br/>
<input type="text" name="number1"><br/>
<input type="text" name="number2"><br/>
<input type="text" name="number3"><br/><br/>
<input type="submit" value="确定">
</form>
</body>
对数据进行判断和转发
String number1=request.getParameter("number1");
String number2=request.getParameter("number2");
String number3=request.getParameter("number3");
/*try{ //把字符串转换为int
Double a=Double.parseDouble(number1);
Double b=Double.parseDouble(number2);
Double c=Double.parseDouble(number3);
if((a>0) && (b>0) && (c>0) && ((a+b)>c) && ((a+c)>b) && ((b+c)>a) ){
RequestDispatcher dispatcher= request.getRequestDispatcher("result1");
dispatcher.forward(request,response);
}
else{
response.sendRedirect("input.jsp");
}
}
catch(NumberFormatException e){
response.sendRedirect("input.jsp");
}*/
try{
int x=Integer.parseInt(number1);
int y=Integer.parseInt(number2);
int z=Integer.parseInt(number3);
if(x+y<=z||x+z<=y||y+z<=x)
response.sendRedirect("input.jsp");
else{
RequestDispatcher dispatcher= request.getRequestDispatcher("/result1");
dispatcher.forward(request,response);
}
}
计算周长和面积
(海伦公式:S=(p(p-x)(p-y)(p-z))½)
String number1=request.getParameter("number1");
String number2=request.getParameter("number2");
String number3=request.getParameter("number3");
int x=Integer.parseInt(number1);
int y=Integer.parseInt(number2);
int z=Integer.parseInt(number3);
int p=(x+y+z)/2;
out.println("三角形的周长是:"+(x+y+z));
out.println("三角形的面积是:"+Math.sqrt(p*(p-x)*(p-y)*(p-z)));