Using Recursion to add an array of doubles
807597Nov 16 2005 — edited Nov 16 2005I have a school project due in an hour and a half and I have NO idea what i'm doing wrong.
we're supposed to take in doubles as long as the user dosnt enter 0 then find the sum of those variables in the array. I
've got it reading the info and putting it into an array but for some reason I can't get the simple recursion to add up all the doubles.
the class to get the sums is computeSumAtEven. and I can't use any loops or static variables to do this. PLEASE help me.
thankyou
-James
import java.util.Scanner;
public class Assignment9 {
public static void main(String[] args)
{
double list[] = new double[100];
Scanner scan = new Scanner(System.in);
boolean end = false;
int i = 0, times = 0;
String temp;
do
{
temp = scan.next();
if((Double.parseDouble(temp)) != 0)
{
list[i] = Double.parseDouble(temp);
}else{
end = true;
}
times++;
}while(end == false);
System.out.println(computeSumAtEven(list, times));
}
public static double computeSumAtEven(double[] numbers, int i)
{
if(i == 0)
{
return numbers;
}
else{
return numbers[i]+computeSumAtEven(numbers,i-1);
}
}
}