错误:在这个范围内没有声明' n '。

时间:2022-10-03 09:44:32

I wrote a function to find the sum of divisors for a number n.

我写了一个函数来求出n的因数的和。

int divisor_sum(long n) {
    long sum = 0;
    for (int a=1, a<=n, a++) {
        if n % a == 0 {
            sum = sum + a;
        }
    }
    return sum;
}

Unfortunately, the program (which includes a main function skeleton) won't compile because it says that "'n' was not declared in this scope." I've tried declaring n as a long before and after the function definition statement to no avail. How do I fix this? Thanks

不幸的是,这个程序(包含一个主函数骨架)不会编译,因为它说“‘n’没有在这个范围内声明。”我试着在函数定义语句之前和之后都声明n是无效的。我怎么解决这个问题?谢谢

1 个解决方案

#1


0  

Like StoryTeller and O'Neil told you in the comments, you need to replace this

就像StoryTeller和O'Neil在评论中告诉你的,你需要替换掉这个。

for (int a=1, a<=n, a++)

with

for (int a = 1; a <= n; a++)

and this

if n % a == 0

with

if (n % a == 0)

#1


0  

Like StoryTeller and O'Neil told you in the comments, you need to replace this

就像StoryTeller和O'Neil在评论中告诉你的,你需要替换掉这个。

for (int a=1, a<=n, a++)

with

for (int a = 1; a <= n; a++)

and this

if n % a == 0

with

if (n % a == 0)