简体   繁体   English

我不明白为什么sum = 0?

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

I'm studying a Java course in Udemy. 我正在Udemy学习Java课程。 We have a challenge question, i understand most of the code but not this: sum= 0; 我们有一个挑战性的问题,我理解大多数代码,但不是这样:sum = 0;

I've asked the teacher on Udemy but no answer. 我已经问过有关Udemy的老师,但没有答案。

So here's the challenge: Write a method called isOdd with an int parameter and call it number. 所以这是挑战:写一个带有int参数的isOdd方法,并将其命名为number。 The method needs to return a boolean. 该方法需要返回一个布尔值。 Check that number is > 0, if it is not return false. 如果没有返回false,请检查该数字是否大于0。 Write a second method called sumOdd that has 2 int parameters: start and end, whoch reperesent a range of numbers. 编写另一个名为sumOdd的方法,该方法具有2个int参数:start和end,叉号代表一定范围的数字。 The method should use a for loop to sum all odd numbers in that range including the "end" and return the sum. 该方法应使用for循环来求和该范围内的所有奇数,包括“ end”,然后返回总和。 The method is called isOdd to check if each number is odd. 该方法称为isOdd,以检查每个数字是否为奇数。 The parameter end needs to be greater than or equal to start and both start and end parameters have to be greater than 0. 参数end必须大于或等于start,并且start和end参数都必须大于0。

If those methods are not satisfied, return -1. 如果不满足这些方法,则返回-1。

In the challenge on udemy if i don't type sum = 0 ie: sum = 10 it gives an error. 在对udemy的挑战中,如果我不键入sum = 0,即:sum = 10,则会产生错误。 I don't get why sum = 0? 我不明白为什么总和= 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 . = 0部分是变量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. 至于为什么必须将变量专门初始化为0,这是因为它是使其余方法实现按需工作的正确值。 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. 从语义上讲, sum记录了到目前为止已处理的奇数的连续总和,并且在处理任何奇数之前,到目前为止已处理的奇数的确应为零。 That may even be your method's final return value, such as when you invoke SumOddRange.sumOdd(2,2) . 甚至可能是方法的最终返回值,例如调用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. 对一系列数字求和时,必须首先使用标识值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. 如果您使用的是一系列数字的乘积,则会将prod初始化为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. 应该返回-1,因为13 <15,这是您的方法中的错误。

You have to create the variable sum to start at 0. It's like if you were counting marbles or something. 您必须创建变量sum,使其从0开始。这就像您在计算大理石或其他东西一样。 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. 在计数开始时没有人说过0,因为很明显您将从0开始。但是,计算机不知道该值,因此您必须在循环开始时告诉它当前总和为0。然后,您可以使用循环并在计数时加和。

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. 如果您不说sum为0,那么在循环中说“ sum + = i”时,您将得到一个错误,因为计算机不知道以什么开头。

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

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