递归方法,返回从0到int的数字之和,递增1

时间:2015-01-31 07:00:08

标签: recursion methods increment

如果遇到递归方法的问题,它应该输出递增1的数字之和,以得到在命令行参数中输入的内容(对不起,如果措辞有点奇怪)

例如,如果将10输入到args中,那么它应该输出55 这是我的代码,有问题的方法是sumofNumbers

public class RecursionMethods {


public static void main(String[] commandlineArguments) {
if (commandlineArguments.length == 0) {
    System.out.println("Please enter a least one commandline!");
   } 
else {
    Integer number = new Integer(0); // initializes the number
    try {
        number = Integer.parseInt(commandlineArguments[0]);
    } catch (NumberFormatException exception) { // NumberFormatException
        System.out.println(exception + " is not a integer.");
        System.exit(1); // ends the program
    }
    String asterisk = asterisks(number); // return address for asterisks
    System.out.println(asterisk);

    String reverse = reverses(number);
    System.out.println(reverse);

    String counting = count(number);
    System.out.println(counting);

    int sums = sumofNumbers(number);
    System.out.println(sums);

 }

}

  public static String asterisks (Integer number) {
  if (number == 0) {
    return "";
} else {
    return "*" + asterisks(number - 1);
  }
}

public static String reverses (Integer number) {
if (number == 0) {
    return "0";
  }
  return number + " " + reverses(number - 1);
 } 

 public static String count (Integer number) {
if (number == 0) {
    return "0";
  }
 return count(number - 1) + " " + number;  
}       
  //here is where the recursion im having trouble with would go 
 public static int sumofNumbers (Integer number) {
   if (number == 0) {
    return 0;  
   }
  return sum(            






}   

0 个答案:

没有答案