Closed. This question is off-topic。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,因此它是on-topic,用于堆栈溢出。
                        
                        去年关闭。
                                                                                            
                
        
为了弄清楚我的代码出了什么问题,请有人帮帮我。该代码应该能够找出并打印出介于1到1000之间的理想数字以及数字的因数。我正在尝试调用perfect(number)函数,但始终弹出错误消息“需要Lvalue作为赋值的左操作数”。

#include <stdio.h>

int perfect(int number);

int main(void){

    printf("The perfect numbers between 1 and 1000 are\n:");

    for (int number = 1; number <=1000; number++){
        perfect(number);
        if (perfect(number) = 1){
            printf("%d is a perfect number; ", number);
            printf("It's factors are = ");
                for (int i = 1; i <= number; i++){ //might need to change i<number
                    if (number % i == 0){
                        printf("%d", i);
                    }
                }
        }
    }
    return 0;
}

int perfect(int number){

    int sum;

    for(int number = 1;number <= 1000; number++){
        sum = 0;

        for (int i = 1; i <= number/2; i++){
            if (number % i == 0){
                sum = sum + i;
            }
        }

        if (sum == number){
           return 1;
        }
        else {
            return 0;
        }
    }

}

最佳答案

在C(以及其他借用其语法的其他语言)中,=是赋值运算符,而不是相等比较运算符。将if (perfect(number) = 1)更改为if (perfect(number) == 1)

关于c - 调用函数时需要左值作为赋值的左操作数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54395884/

10-12 16:07