Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 为什么我的程序没有给出正确的输出?_C - Fatal编程技术网

C 为什么我的程序没有给出正确的输出?

C 为什么我的程序没有给出正确的输出?,c,C,逻辑上哪里错了?我没有发现它不正确,但它的输出是正确的 给出的值仅为1。它应该给出从1到500的所有阿姆斯特朗数字 #include<stdio.h> #include<conio.h> void main() { clrscr(); int a,b,c=0 ,d,i=1; while(i<=500) { b=i;

逻辑上哪里错了?我没有发现它不正确,但它的输出是正确的 给出的值仅为1。它应该给出从1到500的所有阿姆斯特朗数字

     #include<stdio.h>
     #include<conio.h>
     void main()
         {
         clrscr();
         int a,b,c=0 ,d,i=1;
         while(i<=500)
             {
             b=i;
             while(b>0)
                 {
                 a=b%10;
                 c=(a*a*a)+c;
                 b=b/10;
                 }
             if(c==i)
    `            printf("%d",i);
             i++;
             }
          getch();
          }  
您需要在内部循环之前初始化c:

 while(i<=500)
 {
     b=i;
     c=0;    /* reset 'c' */
     while(b>0)
     {
        a=b%10;
        c=(a*a*a)+c;
        b=b/10;
     }
}

您正在对main使用非标准签名。如果运行以下代码,请参阅:

您将看到为什么只有一个输出

注:正确声明主要参数

注意:使用通用函数而不是专有的conio.h

注意:使用简单的“for”语句而不是“while”和“i”的增量

#include <stdio.h>
#include <stdlib.h>
//#include<conio.h>

int main()
{
    //clrscr();
    int a;
    int b;
    int c=0;
    int i=1;

    for( ; i<=500; i++ )
    {
        b=i;

        while(b>0)
        {
            a=b%10;
            c=(a*a*a)+c;
            b=b/10;
        }

        printf( "a=%d. b=%d, c=%d\n", a,b,c);
        if(c==i)
            printf("%d\n",i);
    } // end for

    //getch();
    getchar();
    return(0);
} // end function main

首先,voidmain应该给出一个警告,您正在使用哪个编译器?第二个conio.h不是标准头,getch不是标准函数,getch在程序中也不需要,在环境中也需要,这对您的环境有什么影响?有人能帮助您查找程序的逻辑或格式不正确的地方吗?请尽可能多地使用printf语句,并在您不知道如何使用调试器的情况下查找缺少逻辑的地方。1是c==i的唯一时间,请尝试打印c。另外,在printf中的数字后面加一个空格或其他分隔符,否则您将不知道一个数字的结尾和下一个数字的开头,printf%d,i;。您知道没有使用d吗?只有两种有效的和一种可选的方法来声明main all方法返回“int”。强烈编译,为gcc启用所有警告,至少使用-Wall-Wextra-pedantic,但我已经在beginning@anindya每次检查新号码时,都必须对其进行初始化。是。但这还不够。一旦c变为某个值,您需要在找到下一个阿姆斯特朗编号之前将其重置为0。否则,您将继续添加到上一个c中,这将不会产生正确的结果。然后,它不会将其添加到一个数字的上一个立方体和中。您是对的