简体   繁体   English

我已经在 function 的顶部用零初始化了一个变量,所以在循环期间值会改变吗?

[英]i have initialized a variable with zero at the top of the function so during a loop the value changes?

public class maxsubarraysum {

    public static void main(String[] args) {
        int numbers[] = { 1, -2, 6, -1, 3 };
        printmsasum(numbers);
    }

    public static void printmsasum(int numbers[]) {
        int currsum=0;//declared and initialized
        int maxsum = Integer.MIN_VALUE;
        for (int i = 0; i < numbers.length; i++) {
            int start = i;
            for (int j = i; j < numbers.length; j++) {
                int end = j;
                 currsum = 0;                    //here is what i dont understand why i have to again give it zero to run it properly what its is called ? means am i missing any concept? pls help in loops does value changes?
                for (int k = start; k <= end; k++) {
                    currsum += numbers[k];
                }
                System.out.println(currsum);
                if (maxsum < currsum) {
                    maxsum = currsum;
                }
            }
        }
        System.out.println("the maximum sub array sum is = " + maxsum);
    }

}

i tried it with only declaring and initializing the currsum variable with zero,then the output is wrong then inside the second nested loop why i have to initialized it with zero for correct answer?我尝试过只用零声明和初始化 currsum 变量,然后 output 是错误的然后在第二个嵌套循环中为什么我必须用零初始化它以获得正确答案?

You are declaring cursum in the function so its scope is the entire function, which contain all the loops.您在 function 中声明 cursum,因此它的 scope 是整个 function,其中包含所有循环。 It will not be reset to 0 on entering your inner loop as its scope is outside of that loop.它不会在进入您的内部循环时重置为 0,因为它的 scope 在该循环之外。

You could just move its declaration inside the second loop, right before your "k" loop.您可以将其声明移到第二个循环内,就在“k”循环之前。 That will restrict its scope to the iteration over k which looks to be the partial sum you want.这会将其 scope 限制为对 k 的迭代,这看起来是您想要的部分和。

That would be replacing your那将取代你的

cursum = 0; 

with

int cursum = 0;

You are changing currsum in every iteration currsum += numbers[k];您在每次迭代中都在更改currsum currsum += numbers[k]; , so to check every new result, you have to reset the currsum . ,因此要检查每个新结果,您必须重置currsum You don't have to declare it before loops, you can actually declare it in the same place you are setting it to 0.您不必在循环之前声明它,您实际上可以在将其设置为 0 的同一位置声明它。

Tip: You also don't have to initalize start and end variables, you can use i and j提示:您也不必初始化startend变量,您可以使用ij

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么在 for 循环中初始化变量时,我会收到“变量可能没有为变量 a、b 和 n 初始化”? - Why am I getting "variable might not have initialized for variables a,b and n" while they are initialized in a for-loop? 局部变量可能尚未初始化并循环 - local Variable may not have been initialized and a loop StringBuilder循环:本地变量可能尚未初始化 - StringBuilder loop: Local variable may not have been initialized 在循环中初始化双精度会给出错误“变量可能尚未初始化” - Initializing doubles in a loop giving error “variable might not have been initialized” 我的 java 方法返回语句有问题,我在 if-else 语句中为未初始化的变量赋值 - I have a problem with a java method return statement, I have given value for an un-initialized variable in an if-else statement 为什么变量必须初始化? - why variable have to be initialized? 要循环,但变量未初始化 - Want to loop but variable is not initialized 变量未在for循环中初始化 - Variable not initialized in for loop 我的返回值有一个错误,它说它没有初始化,但是我在它上面的if then语句中将它初始化了 - I have an error with my return value, it says it isn't initialized but I have it initialized in a if then statement right above it 变量birthDate 可能没有被初始化……但我认为我做到了? - Variable birthDate might not have been initialized… but I thought I did?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM