我正在编写一个使用一系列数字进行输入的程序。使用递归和数组我试图添加所有输入的可被 3 整除的数字并将它们加在一起。前任。3 4 5 6 输出应该是 9。我当前的输出一直给我 3 作为我的条目的输出。有什么帮助或建议吗?
import java.io.*;
import java.text.*;
public class Assignment9 {
public static void main (String args[]) throws IOException{
int i = 0;
int [] nums;
nums = new int [100];
InputStreamReader inRead = new InputStreamReader(System.in);
BufferedReader buffRead = new BufferedReader(inRead);
String line = buffRead.readLine();
try {
while (line.equals("0") == false && i<100) {
i++;
line = buffRead.readLine();
nums[i]=(int) Double.parseDouble(line);
}
} catch(IOException e) {
System.out.println("Array index out of bound");
}
int endIndex = computeSumDivisibleBy3(nums, 0, nums.length-1);
System.out.print ("The minimum number is " + min + ('\n'));
System.out.print ("The sum of the numbers divisible by 3 is " + endIndex + ('\n'));
}
}
public static int computeSumDivisibleBy3(int [] numbers, int startIndex, int endIndex) {
if(startIndex == endIndex) {
if(numbers[endIndex] %3 == 0){
return (int) numbers[endIndex];
} else {
return 0;
}
} else {
if(numbers[endIndex] %3 == 0) {
return (int) (computeSumDivisibleBy3(numbers, startIndex, endIndex - 1) + numbers
}
else {
return computeSumDivisibleBy3(numbers, startIndex, endIndex - 1);
}
}
}