【问题标题】:Im a really stumped on this Methods and Recursion我对这种方法和递归感到非常困惑
【发布时间】:2018-03-13 11:35:49
【问题描述】:

所以我不太确定我已经做了什么 /* * 要完成的方法集合 * 完成给定方法的每个 TODO 正文代码 * 对第一种方法和最后一种方法使用递归 */

public class Methods 
  /*
   * method to compute value of f(n) where:
   * f(1) = 1
   * f(n) = n + f(n-1) for n>1, n is even
   * f(n) = n * f(n-1) for n>1, n is odd
   */
  public static int f(int n) {
    //TODO

    return 0; //dummy line, replace this
  }

  /*
   * method to compute the sum of the proper divisors of a given positive integer, n
   * e.g., if n is 12, the sum of the proper divisors is:
   * 1 + 2 + 3 + 4 + 6 = 16
   * note: proper divisors are all divisors of an integer other than itself
   */
  public static int sumOfDivisors(int n) {
    //TODO

    return 0; //dummy line, replace this
  }


  /*
   * method that returns a String indicating whether a given positive integer, n, is:
   * "abundant" - sum of proper divisors is greater than n
   * "deficient" - sum of proper divisors is less than n  
   * "perfect" - sum of proper divisors is equal to n
   */
  public static String numberType(int n) {
    //TODO

    return "foo"; //dummy line, replace this
  }

  /*
   * method that returns the sum of the digits of the positive integer, n
   * e.g., if n = 5403, the method will return:
   * 5+4+0+3 = 12
   * note: the right-most (1's) digit can be found using n%10
   * the remaining digits (all but the 1's digit) can be found using n/10
   */
  public static int sumOfDigits(int n) {
    //TODO

    return 0;  //dummy line, replace this
  }

  //a dummy main method, not used
  public static void main(String[] args) {
    System.out.println("This program is not meant to be run on its own.");
    System.out.println("This is just a dummy main method.");
  }

} //end Methods

所以是的,任何帮助都会很棒。

【问题讨论】:

  • 一般来说 SO 不是一个其他开发人员只是在你的代码中填写空白行的网站。

标签: java recursion methods


【解决方案1】:

所以我不太确定我已经做了什么

您实际上只编写了方法声明。 您的任务确实有据可查,您可以尝试实现实用程序(如 public int[] divisors(int n) 等),并最终回到这里就您的问题寻求帮助,发布代码或错误。

PS:这似乎是一项学术作业,你查过一些书或笔记吗?

【讨论】:

    猜你喜欢
    • 2015-09-09
    • 2011-05-13
    • 2013-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多