返回语句可以是ittarted吗? [英] Does return statement can be ittarted?

查看:85
本文介绍了返回语句可以是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")


执行 return 时,它结束当前例程,无论如何它的位置。

要返回超过1的值,你必须返回一个像数组(或指针)的东西。



即使你删除从它的位置返回,我担心你的代码不能达到预期的效果。



当你不明白你的代码在做什么或为什么它做了什么呢是的,答案是调试器

使用调试器查看代码正在做什么。它允许您逐行执行第1行并在执行时检查变量,这是一个令人难以置信的学习工具。



调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]



调试器在这里显示你的内容代码正在做,你的任务是与它应该做的事情进行比较。

调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。
When a return is executed, it ends the current routine, no matter its position.
To return more than 1 value, you must return something like an array (or pointer to.

Even if you remove the return from its place, I fear your code will not do what you expect.

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. It allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆