Struts2计算三角形周长面积

web.xml



    
        struts2
        org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
    
    
        struts2
        /*
    

struts.xml






    
        
            /result.jsp
            /index.jsp
        
    

CountAction

package com.Action;

import com.opensymphony.xwork2.ActionSupport;

public class CountAction extends ActionSupport {
   private String first;
   private String second;
   private String third;
   private double perimeter;
   private double area;
   private String info;

   public String getFirst() {
      return first;
   }

   public void setFirst(String first) {
      this.first = first;
   }

   public String getSecond() {
      return second;
   }

   public void setSecond(String second) {
      this.second = second;
   }

   public String getThird() {
      return third;
   }

   public void setThird(String third) {
      this.third = third;
   }

   public double getPerimeter() {
      return perimeter;
   }

   public void setPerimeter(double perimeter) {
      this.perimeter = perimeter;
   }

   public double getArea() {
      return area;
   }

   public void setArea(double area) {
      this.area = area;
   }

   public String getInfo() {
      return info;
   }

   public void setInfo(String info) {
      this.info = info;
   }

   //   计算周长
   public static double countPerimeter(double x1, double x2, double x3) {
      return x1 + x2 + x3;
   }

   //   计算面积
   public static double countArea(double x1, double x2, double x3) {
      double p = (x1 + x2 + x3) / 2;
      return Math.sqrt(p * (p - x1) * (p - x2) * (p - x3));
   }

   @Override
   public String execute() throws Exception {
      double x1 = Double.parseDouble(getFirst());
      double x2 = Double.parseDouble(getSecond());
      double x3 = Double.parseDouble(getThird());
      String ret = ERROR;
      if (x1 + x2 > x3 && x1 + x3 > x2 && x2 + x3 > x1) {
         info = "计算结果为:";
         perimeter = Math.round(countPerimeter(x1, x2, x3) * 100) / 100;
         area = Math.round(countArea(x1, x2, x3) * 100) / 100;
         ret = SUCCESS;
      } else {
         info = "您输入的三条边不满足三角形条件";
      }
      return ret;
   }
}

index.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<%--
  Created by IntelliJ IDEA.
  User: Elijah
  Date: 2018/9/27
  Time: 17:42
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    计算三角形周长和面积




    
    
    
    
    





result.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<%--
  Created by IntelliJ IDEA.
  User: Elijah
  Date: 2018/9/27
  Time: 17:46
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    计算结果



周长:

面积:



 

你可能感兴趣的:(JavaWeb)