【发布时间】:2020-09-05 23:41:33
【问题描述】:
我在将 while 循环转换为递归时遇到问题...该循环似乎工作正常,但是我尝试了几次将其转换为递归,并且该方法返回的最后一个(返回 c;)为 0 ...我的意思是你怎么能把一个while循环变成一个递归?程序应该计算数组中小于 2 的数字
这是主要的
public static void main(String[] args) {
double[] gpa = new double[]{2.5, 1.3, 1.3, 3.3, 1.2, 3.2, 4, 2.3, 3.1, 1.2};
int start = 0;
countGPA(gpa,start);
System.out.println(countGPA(gpa, start));
}
这就是方法
public static int countGPA(double[] gpas, int start) {
int L = gpas.length;
int c = 0;
int countGPA = c;
while (start < 10) {
if (start > 0 && start != L) {
if (gpas[start] < 2.0 && gpas[start] > 0) {
c++;
}
} else if (start == L) {
return 0;
} else if (start < 0 && start > L) {
return -1;
}
start++;
}
return c;
}
【问题讨论】:
-
向我们展示您的尝试?