java 传一个入参total,在递归方法中做累加计算,计算完成后,获取计算好的total
时间: 2024-01-15 10:03:22 浏览: 25
可以将total作为参数传递给递归方法,并在每次递归调用时进行累加操作。当递归结束后,方法会返回最终的total值。示例代码如下:
```java
public int calculateTotal(int n, int total) {
if (n == 0) {
return total;
}
total += n;
return calculateTotal(n - 1, total);
}
```
在调用该方法时,需要传入两个参数:n表示递归的次数,total表示累加的初始值。例如:
```java
int result = calculateTotal(10, 0);
System.out.println(result); // 输出55
```
上述代码会将1到10的数累加起来,最终输出55。
相关问题
java 实现 父子部门的人数 ,父部门人数 为各子部门人数总和 ,和输出各自节点人数
假设我们有一个部门树形结构,每个节点包含一个部门对象和一个子节点列表。部门对象中包含一个部门名称和一个人数。
首先我们需要定义一个递归函数来计算每个节点的人数:
```java
public int calculateTotalEmployees(DepartmentNode node) {
int total = node.getDepartment().getEmployeeCount();
for (DepartmentNode child : node.getChildren()) {
total += calculateTotalEmployees(child);
}
node.getDepartment().setEmployeeCount(total); // 更新当前节点的员工数
return total;
}
```
这个函数首先会获取当前节点的员工数,然后递归调用子节点的这个函数,并将子节点的返回值累加到总人数中。最后更新当前节点的员工数并返回总人数。
接下来我们可以遍历整个部门树,对每个节点调用上面的函数来计算员工数:
```java
public void printDepartmentEmployeeCount(DepartmentNode root) {
calculateTotalEmployees(root); // 先计算每个节点的员工数
printEmployeeCount(root); // 输出每个节点的员工数
}
private void printEmployeeCount(DepartmentNode node) {
System.out.println(node.getDepartment().getName() + ": " + node.getDepartment().getEmployeeCount());
for (DepartmentNode child : node.getChildren()) {
printEmployeeCount(child);
}
}
```
这个函数会先调用 `calculateTotalEmployees` 函数来计算每个节点的员工数,然后遍历整个部门树,输出每个节点的员工数。注意输出时要输出节点名称和员工数。
完整代码如下:
```java
public class DepartmentNode {
private Department department;
private List<DepartmentNode> children;
public DepartmentNode(Department department) {
this.department = department;
children = new ArrayList<>();
}
public Department getDepartment() {
return department;
}
public void addChild(DepartmentNode child) {
children.add(child);
}
public List<DepartmentNode> getChildren() {
return children;
}
}
public class Department {
private String name;
private int employeeCount;
public Department(String name, int employeeCount) {
this.name = name;
this.employeeCount = employeeCount;
}
public String getName() {
return name;
}
public int getEmployeeCount() {
return employeeCount;
}
public void setEmployeeCount(int employeeCount) {
this.employeeCount = employeeCount;
}
}
public class Main {
public static void main(String[] args) {
DepartmentNode root = new DepartmentNode(new Department("总部", 0));
DepartmentNode child1 = new DepartmentNode(new Department("子部门1", 10));
DepartmentNode child2 = new DepartmentNode(new Department("子部门2", 20));
DepartmentNode child3 = new DepartmentNode(new Department("子部门3", 30));
root.addChild(child1);
root.addChild(child2);
root.addChild(child3);
DepartmentNode grandChild1 = new DepartmentNode(new Department("孙子部门1", 5));
DepartmentNode grandChild2 = new DepartmentNode(new Department("孙子部门2", 15));
DepartmentNode grandChild3 = new DepartmentNode(new Department("孙子部门3", 25));
child1.addChild(grandChild1);
child2.addChild(grandChild2);
child3.addChild(grandChild3);
Main main = new Main();
main.printDepartmentEmployeeCount(root);
}
public int calculateTotalEmployees(DepartmentNode node) {
int total = node.getDepartment().getEmployeeCount();
for (DepartmentNode child : node.getChildren()) {
total += calculateTotalEmployees(child);
}
node.getDepartment().setEmployeeCount(total); // 更新当前节点的员工数
return total;
}
public void printDepartmentEmployeeCount(DepartmentNode root) {
calculateTotalEmployees(root); // 先计算每个节点的员工数
printEmployeeCount(root); // 输出每个节点的员工数
}
private void printEmployeeCount(DepartmentNode node) {
System.out.println(node.getDepartment().getName() + ": " + node.getDepartment().getEmployeeCount());
for (DepartmentNode child : node.getChildren()) {
printEmployeeCount(child);
}
}
}
```
阅读全文