Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用for循环方法和递归方法计算连续整数?_Java_Loops_For Loop_Recursion_Int - Fatal编程技术网

Java 如何使用for循环方法和递归方法计算连续整数?

Java 如何使用for循环方法和递归方法计算连续整数?,java,loops,for-loop,recursion,int,Java,Loops,For Loop,Recursion,Int,我需要计算1.)for循环方法和2.)递归方法中连续整数的和。这两种方法都是静态int方法,接受两个int参数(一个是起始int,另一个是其后的int数)。例如,如果输入(3,3),输出应该是18,因为3是起始数字,后面的3个整数是4,5和6。当你把这些加起来(3+4+5+6),你得到18。这两个方法都在做相同的数学运算,除了一个是使用for循环,另一个是递归进行 我在这里遇到的问题是我的for-loop方法不能正确地总结。当我输入(3,3)时,输出是31。此外,我不太确定如何编写递归方法,因为

我需要计算1.)for循环方法和2.)递归方法中连续整数的和。这两种方法都是静态int方法,接受两个int参数(一个是起始int,另一个是其后的int数)。例如,如果输入(3,3),输出应该是18,因为3是起始数字,后面的3个整数是4,5和6。当你把这些加起来(3+4+5+6),你得到18。这两个方法都在做相同的数学运算,除了一个是使用for循环,另一个是递归进行

我在这里遇到的问题是我的for-loop方法不能正确地总结。当我输入(3,3)时,输出是31。此外,我不太确定如何编写递归方法,因为我的for循环方法不起作用。我能得到一些帮助吗

而且,没有数组或数组列表。此代码应该能够在不使用这些代码的情况下工作

public static int consecSum(int startingNum, int numInts){
    for (int i = numInts; i>0; i--){
        startingNum += (startingNum + 1);
    }
    return startingNum;
}

迭代解决方案的问题在于每次迭代都要修改起点。相反,可以为总和使用单独的变量:

public static int consecSum(int startingNum, int numInts){
    int sum = 0;
    for (int i = numInts; i>0; i--){
        sum += (startingNum + 1);
    }
    return sum;
}
递归实现有一个类似的问题:

public static int recursSum(int startingNum, int numInts) {
    if (numInts == 0) {
        return startingNum;
    }
    return startingNum + recursSum(startingNum + 1, numInts - 1);
}

迭代方法:

public static int consecSum(int startingNum, int numInts){
        int sum = startingNum++;
        for (int i = numInts; i>0; i--, startingNum++;){
            sum += startingNum;
        }
        return sum;
    }
public static int recursSum(int startingNum, int numInts) {
        if (numInts < 0) {
            return startingNum;
        }
        return startingNum + recursSum(startingNum+1, numInts - 1);
}
递归方法:

public static int consecSum(int startingNum, int numInts){
        int sum = startingNum++;
        for (int i = numInts; i>0; i--, startingNum++;){
            sum += startingNum;
        }
        return sum;
    }
public static int recursSum(int startingNum, int numInts) {
        if (numInts < 0) {
            return startingNum;
        }
        return startingNum + recursSum(startingNum+1, numInts - 1);
}
publicstaticintrecursum(intstartingnum,intnumints){
if(numInts<0){
返回startingNum;
}
返回startingNum+recursesum(startingNum+1,numInts-1);
}
concersum()
中,您正在使用
startingNum+=(startingNum+1)
更新循环中的
startingNum

您应该为结果使用一个新变量:

public static int consecSum(int startingNum, int numInts) {
    int result = startingNum;
    for (int i = numInts; i > 0; i--) {
        result += (startingNum + i);
    }
    return result;
}
在您的
recursum
中,您正在每个迭代中添加
startingNum+numInts
。只需添加
1

public static int recursSum(int startingNum, int numInts) {
    if (numInts <= 0) {
        return startingNum;
    }
    return startingNum + recursSum(startingNum + 1, numInts - 1);
}
publicstaticintrecursum(intstartingnum,intnumints){
if(循环的最小值)
for循环解决方案中的问题是,您认为“最后一个整数”实际上是“最后一个和”

startingNum = 3
startingNum = 3 + 4
startingNum = 7 + 5
startingNum = 12 + 6
但是,由于您总是将新的总和保存在
startingNum
本身中,因此发生了这样的情况

startingNum = 3
startingNum = 3 + 4
startingNum = 7 + 8 (because startingNum + 1 = 7 + 1 = 8)
startingNum = 15 + 16
试试这个

public static int consecSum(int startingNum, int numInts){
    int nextNum = startingNum + 1;
    for (int i = numInts; i>0; i--){
        startingNum += nextNum;
        nextNum++;
    }
    return startingNum;
}
递归 你几乎做到了。根据我所看到的,你的想法是如果整数的数目是0,就返回起始数,否则返回起始数+下一个数调用的方法的输出。这绝对有效。请尝试这些编辑

public static int recursSum(int startingNum, int numInts) {
    if (numInts == 0) {
        // eventually the numInts will become 0, meaning there's no
        // numbers to add after this startingNum, so just return it
        return startingNum;
    }

    // otherwise, if numInts > 0, that means there are other numbers to add so
    // return the sum of the current number with the output of the function called
    // on the next number (while not forgetting to decrease the number of integers
    // we should consider after that)
    return startingNum + recursSum(startingNum + 1 /* next number */, numInts - 1 /* decrease the number of integers to consider after that */);
}