简体   繁体   中英

I don't understand the why is sum = 0?

I'm studying a Java course in Udemy. We have a challenge question, i understand most of the code but not this: sum= 0;

I've asked the teacher on Udemy but no answer.

So here's the challenge: Write a method called isOdd with an int parameter and call it number. The method needs to return a boolean. Check that number is > 0, if it is not return false. Write a second method called sumOdd that has 2 int parameters: start and end, whoch reperesent a range of numbers. The method should use a for loop to sum all odd numbers in that range including the "end" and return the sum. The method is called isOdd to check if each number is odd. The parameter end needs to be greater than or equal to start and both start and end parameters have to be greater than 0.

If those methods are not satisfied, return -1.

In the challenge on udemy if i don't type sum = 0 ie: sum = 10 it gives an error. I don't get why sum = 0?

class SumOddRange {
    public static void main(String[] args) {
        sumOdd(15, 13);
        isOdd(10);
    }
    public static boolean isOdd(int number) {
        if (number < 0) {
            return false;
        } else if (number % 2 != 0) {
            return true;
        } else {
            return false;
        }
    }

    public static int sumOdd(int start, int end) {
        if((end < start) || (start <= 0)) {
            return -1;}
        int sum = 0;
        for (int i = start; i <= end; i++) {
            if (isOdd(i)) {
                sum += i;

            }
        }
        return sum;
    }

}

In

 int sum = 0; 

the = 0 part is an initialization of variable sum . That is, it specifies that variable's initial value, almost exactly as if you instead wrote

int sum;
sum = 0;

Local variables have no defined value (and you may not use their values) until a value is first assigned, so it is necessary to provide an initial value via one of those two forms.

As for why you must specifically initialize that variable to 0, that's because it's the correct value to make the rest of your method implementation work as required. If you initialize it differently then the method will return a different value for the same arguments.

Semantically speaking, sum records a running sum of the odd numbers processed so far, and before you've processed any, the sum of those processed so far should indeed be zero. That may even be your method's final return value, such as when you invoke SumOddRange.sumOdd(2,2) .

Note: do not confuse the assignment operator, = , with the equality-test operator, == .

When you sum up a series of numbers, you must first initialize the sum with the identity value of 0.

So

int sum = 0;

sum = sum + 10; // now sum is 10
sum = sum + 20; // now sum is 30

If you had been taking the product of a series of numbers would have initialized prod to 1.

int prod = 1;
prod = prod * 5; // now prod is 5
prod = prod * 7; // now prod is 35 

Note that you need to assign a value to accept the sum. So

int sum = sumOdd(15,13);

should return -1 since 13 < 15 which is an error in your method.

You have to create the variable sum to start at 0. It's like if you were counting marbles or something. No one ever says 0 at the start of their counting, because it's obvious that you would start at 0. Yet, a computer doesn't know that, so you have to tell it at the beginning of the loop that the current sum is 0. Then you can use the loop and add to your sum as you count.

If you don't say sum is 0, then when you say "sum += i" in the loop, you will get an error, because the computer doesn't know what sum started out as.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM