Using =+ won't work in for loop

后端 未结 1 1350
-上瘾入骨i
-上瘾入骨i 2020-12-04 03:32

This works:

for(int i = 0; i < size; i++){
    avg[0] = avg[0] + array0[i];
    avg[1] = avg[1] + array1[i];
    avg[2] = avg[2] + array2[i];
    avg[3] =         


        
相关标签:
1条回答
  • 2020-12-04 03:48

    it's +=, not =+

    What you do could be valid code as well, but right now you're doing

    avg[0] = + array0[i];
    

    It will work for numeric types (which I assume you have). Simplified example without array index:

    int x = +5;
    

    Sample:

    public static void main(String[] args) {
        int x = -5;
        int y = +x;
        System.out.println(y); // - + => -
    
        int a = 5;
        int b = -a;
        System.out.println(b); // + - => -
    
        int c = 5;
        int d = +5;
        System.out.println(d); // + + => +
    
        int m = -5;
        int n = -m;
        System.out.println(n); // - - => +
    }
    

    Output:

    -5
    -5
    5
    5

    Copied from comments for clarity:

    You're basically saying x = + y. In this case the + is just a matter of indicating it's a positive integer. It's valid code, but it's not what you intend.

    0 讨论(0)
提交回复
热议问题