在 Java 中递归计数 - 简单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18997843/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 13:12:55  来源:igfitidea点击:

Recursively keeping count in Java - Simple

javarecursioncountincrement

提问by Robert Maxwell

I know that I'm overlooking something incredibly fundamental and elementary, but I need help with creating a mean function that, using only one parameter (the list containing the integers- in this case), calculates the mean of the given integers.

我知道我忽略了一些非常基础和基本的东西,但我需要帮助来创建一个均值函数,该函数仅使用一个参数(在这种情况下是包含整数的列表),计算给定整数的均值。

public static double mean (Cons lst) {
    int total = (Integer) lst.data;
    int count = //something to keep count through the recursion

    if(lst.next == null) {
        return total / count;
    }

    else return mean(lst.next); // return statement isn't correct, need help here as well
}

Any help would be great. If the easiest way to explain is by writing the method itself, then that'd be wonderful, but I'm just trying to figure out how to recursively keep a running count without adding params.

任何帮助都会很棒。如果最简单的解释方法是编写方法本身,那就太好了,但我只是想弄清楚如何在不添加 params 的情况下递归地保持运行计数。

Thanks a lot.

非常感谢。

采纳答案by Pablo Francisco Pérez Hidalgo

You are developing your recursive mean function as a method of a Java Class. Why don't you declare your count and total local variables as attributes of that class?

您正在将递归均值函数开发为 Java 类的方法。为什么不将 count 和 total 局部变量声明为该类的属性?

class Mean {

    static int total = 0;
    static int count = 0;

    public static double mean (Cons lst) {
        total += (Integer) lst.data;
        count += 1;
        if(lst.next == null) {
            double ret = total/count;
            total = 0;
            count = 0;
            return ret;
        }
      return mean(lst.next); // return statement isn't correct, need help here as well
    }
}

Other option is to include "count" as a second parameter of your recursive method. If you don't want the user to pass more parameters use two methods: "mean" method, with one parameter (your list), should call the second method "recursiveMean(list, 0)" containing your implementation.

其他选项是将“计数”作为递归方法的第二个参数。如果您不希望用户传递更多参数,请使用两种方法:“mean”方法,带有一个参数(您的列表),应调用包含您的实现的第二种方法“recursiveMean(list, 0)”。

public static double mean (Cons lst) {
    return recursiveMean (lst, 0, 0)
}

public static double recursiveMean (Cons lst, int count, int total) {
    total += (Integer) lst.data;
    count += 1;
    if(lst.next == null) {
        return total / count;
    }
  return mean(lst.next,count,total); // return statement isn't correct, need help here as well
}    

Nevertheless, I don't see why you are implementing a mean function as a recursive function unless it is some kind of educational exercise.

尽管如此,我不明白您为什么要将平均函数实现为递归函数,除非它是某种教育练习。