java获取所选类别中所有项目的平均值

如何获得所选公司所有员工的平均工资? 我首先选择公司,然后传递id并根据该ID,我将所有员工都放在那里并在表格中显示他们的信息。 目标是获得该组中每个人的平均工资。

      <% List employees = company.getEmployees(); double sum=0.0; %>  <% for(int i=0; i  
ID Name Salary
//get the average salary of all employees ::: This is working based on JChris's answer

Average salary of all employees in this company:

//this is returning zero.

New average method:

您可以将工资加起来然后除以员工人数:

 <% List employees = company.getEmployees(); double sum=0.0; %>  <% for(int i=0; i   <% } %> 
ID Name Salary
<%=employees.get(i).getId()%> <%=employees.get(i).getNom()%> <%=employees.get(i).getSalary()%>
//get the average salary of all employees

Average salary of all employees in this company:<%=sum/(double)employees.size()%>

话虽如此,我敦促你在走这条路之前重新考虑一下。 在JSP中包含代码会严重阻碍维护它的机会,如果它超过1-2页。 您的所有业务逻辑都应该在您的代码中发生(例如在您的servlet中),而在JSP中只显示信息。