返回语句可以是ittarted吗

返回语句可以是ittarted吗

本文介绍了返回语句可以是ittarted吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我试图找到范围内的所有阿姆斯特朗号,但它只返回第一号。或者如果return语句不在lop中,那么只返回最后一个。



我尝试过:



this is what i have tried to find all the Armstrong no.s in the range but its return only first no. or if return statement is not in the lop then returns only last no.

What I have tried:

#include<iostream>
using namespace std;
int arm(){
    int x=0,sum,temp,i,a[100]={0};
  for(i=1;i<=500;i++){
    temp=i;
    sum=0;
    while(temp!=0){
        sum=sum+(temp%10)*(temp%10)*(temp%10);
        temp=temp/10;
    }
    if(sum==i){
        for(int l=0;l<=i;l++){
            a[l]=i;
            return a[l];

        }

    }


  }

}
int main(){

cout<<arm();

}

推荐答案

引用:

帮帮我们。



嗯......看看你的代码。

返回声明?

简单:它在你的中为循环!所以它执行数组中的第一个元素,然后立即退出函数:那就是 return 用于:立即退出函数而不进行任何进一步处理。



猜测,您要么更改函数以返回整数数组,并在主函数中打印它们,或者打印值而不是使用 return

就个人而言,我会在函数末尾返回数组,让main处理它。这样,如果需要,您可以稍后重新使用该功能用于其他目的。 (这也称为单一责任原则)


Well... look at your code.
Where is the return statement?
Simple: it's in your for loop! So it does the first element in the array, then immediately exits the function: that's what return is there for: to exit the function immediately without any further processing.

At a guess, you either want to change the function to return an array of integers, and print them in your main function, or print the values instead of using return.
Personally, I'd return the array at the end of the function, and let main handle printing it. That way, you can re-use the function later for other purposes if you need to. (This is also called "the single responsibility principle")



这篇关于返回语句可以是ittarted吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 14:53