它返回一个类似于02212138841293291的值。我知道整数的零被删除,0到11之间的数字之和不会扩大到那么高。语言Java(我道歉并不清楚)
public class Sum1 {
public static void main(String[] args) {
int end = 11;
int start = 0;
int sum = 0;
sums(start, end, sum);
}
public static void sums(int start, int end, int sum) {
if(start<= end) {
sum += start;
start += 1;
sums(start, end, sum);
}
System.out.print(sum);
}
}
答案 0 :(得分:0)
每次从总和中调用“总和”时,您将“总和”重置为0.您需要每次将总和传递给总和。
public class Sum1 {
public static void main(String[] args) {
int end = 11;
int start = 0;
int sum = 0;
sums(start, end, sum);
}
public static void sums(int start, int end, int sum) {
if(start<= end) {
sum += start;
start += 1;
sums(start, end, sum);
}
System.out.print(sum);
}
}