Tengo problemas para invocar un método recursivo estático en Java

I am trying to add up the elements in an array all while using a recursive method. However, I can't execute the method since I get an error. So, since I am using parameters in the static method, is there a way to execute it based on my code?

import java.util.Scanner;
public class Harro {
    public static void main(String[] args) {
        input();
    }

    private static void input() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Lower bound: ");
        int lower = scanner.nextInt();
        System.out.print("Upper bound: ");
        int upper = scanner.nextInt();
        arrayForm(upper, lower);
    }

    private static void arrayForm(int upper, int lower) {
        int b = 0;
        int a = Math.abs(lower) + Math.abs(upper);
        int array[] = new int[a];
        for (int i = 0; i < array.length; i++) {
            array[i] = lower + i;
        }
        summation(array[], b);
    }

    public static int summation(int array[], int b) {
        if (b > array.length) {
            System.out.println("Cannot continue");
            return 0;
        } else{
            int result = array[b] + summation(array, b + 1);
            System.out.println("recursion call: " + b);
            System.out.println("sum: " + result);
            System.out.println("parameter 1: " + array[b]);
            System.out.println("parameter 2: " + array[b + 1]);
            return result;
        }
    }
}

preguntado el 10 de marzo de 12 a las 02:03

Can you please format the code a bit better? -

I receive a '.class expected' where I have summation(array[], b);. am I doing something wrong? -

2 Respuestas

Cambia esta linea

summation(array[], b);

a

summation(array, b);

[] denotes array type, you only need the identifier.

respondido 10 mar '12, 02:03

Yeah you shouldn't call array variables with [] - Chan

  private static void arrayForm(int upper, int lower)
  {
    int b = 0;
        int a = Math.abs(lower) + Math.abs(upper);
    int array[] = new int[a];
        for (int i = 0; i < array.length; i++)
    {
        array[i] = lower + i;
    }
    summation(array, b);
  }

Compile error was on that summation call. Your code now compiles but still have some runtimes errors

respondido 10 mar '12, 02:03

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.