C 为什么我的代码没有给出正确的输出?

C 为什么我的代码没有给出正确的输出?,c,C,这是将m到n范围内的所有阿姆斯特朗数字打印为用户输入的代码。但当我运行它时,它不会给出正确的输出。假设我输入的m为1,n为10000,它只显示“1是阿姆斯特朗”,其他什么都没有。请告诉我我的代码中是否有错误 #include<stdio.h> void main() { int m,n,a,i,j=0,r; printf("Enter m\n"); scanf("%d",&m); printf("Enter n\n"); sca

这是将m到n范围内的所有阿姆斯特朗数字打印为用户输入的代码。但当我运行它时,它不会给出正确的输出。假设我输入的m为1,n为10000,它只显示“1是阿姆斯特朗”,其他什么都没有。请告诉我我的代码中是否有错误

#include<stdio.h>
void main()
{

    int m,n,a,i,j=0,r; 
    printf("Enter m\n");
    scanf("%d",&m);

    printf("Enter n\n");

    scanf("%d",&n);

    for(i=m;i<=n;i++)
    {
        int temp=i;
        while(i>0)
        {
            r=i%10;
            j=j+r*r*r;
            i=i/10;
        }

        if(j==temp)
        {
            printf("%d is armstrong\n",temp);
        }


    }
}
#包括
void main()
{
int m,n,a,i,j=0,r;
printf(“输入m\n”);
scanf(“%d”、&m);
printf(“输入n\n”);
scanf(“%d”和“&n”);
对于(i=m;i0)
{
r=i%10;
j=j+r*r*r;
i=i/10;
}
如果(j==温度)
{
printf(“%d是armstrong\n”,temp);
}
}
}

我在您的程序中看到两个错误,
j
只初始化一次,您正在破坏循环控制
I
。要进行补救,请在循环内初始化
j
,然后使用
temp
而不是
i

#include<stdio.h>

int main(void)                      // modern style
{
    int m, n, i, j, r; 
    printf("Enter m\n");
    scanf("%d", &m);

    printf("Enter n\n");
    scanf("%d", &n);

    for(i = m; i <= n; i++) {
        j = 0;                      // initialise j
        int temp = i;
        while(temp > 0) {           // work with temp
            r = temp % 10;
            j = j + r * r * r;
            temp = temp / 10;
        }

        if(j == i) {
            printf("%d is armstrong\n", i);
        }
    }
}
#包括
int main(void)//现代风格
{
int m,n,i,j,r;
printf(“输入m\n”);
scanf(“%d”、&m);
printf(“输入n\n”);
scanf(“%d”和“&n”);
对于(i=m;i0){//使用temp
r=温度%10;
j=j+r*r*r;
温度=温度/10;
}
如果(j==i){
printf(“%d是armstrong\n”,i);
}
}
}
程序输出:

Enter m 1 Enter n 10000 1 is armstrong 153 is armstrong 370 is armstrong 371 is armstrong 407 is armstrong 输入m 1. 输入n 10000 我是阿姆斯特朗 阿姆斯特朗是谁 阿姆斯特朗是谁 371是阿姆斯特朗 407是阿姆斯特朗
我在您的程序中看到两个错误,
j
只初始化了一次,您正在破坏循环控制
I
。要进行补救,请在循环内初始化
j
,然后使用
temp
而不是
i

#include<stdio.h>

int main(void)                      // modern style
{
    int m, n, i, j, r; 
    printf("Enter m\n");
    scanf("%d", &m);

    printf("Enter n\n");
    scanf("%d", &n);

    for(i = m; i <= n; i++) {
        j = 0;                      // initialise j
        int temp = i;
        while(temp > 0) {           // work with temp
            r = temp % 10;
            j = j + r * r * r;
            temp = temp / 10;
        }

        if(j == i) {
            printf("%d is armstrong\n", i);
        }
    }
}
#包括
int main(void)//现代风格
{
int m,n,i,j,r;
printf(“输入m\n”);
scanf(“%d”、&m);
printf(“输入n\n”);
scanf(“%d”和“&n”);
对于(i=m;i0){//使用temp
r=温度%10;
j=j+r*r*r;
温度=温度/10;
}
如果(j==i){
printf(“%d是armstrong\n”,i);
}
}
}
程序输出:

Enter m 1 Enter n 10000 1 is armstrong 153 is armstrong 370 is armstrong 371 is armstrong 407 is armstrong 输入m 1. 输入n 10000 我是阿姆斯特朗 阿姆斯特朗是谁 阿姆斯特朗是谁 371是阿姆斯特朗 407是阿姆斯特朗 (i=m;i 0)的

{
r=温度%10;
j=j+r*r*r;
温度=温度/10;
}
如果(j==i)
{
printf(“%d是armstrong\n”,i);
}
}
在while循环中,您需要使用变量temp使i保持不变,以便for循环正确地继续。在开始新的while循环之前,将j=0设置为从0开始再次计数。在if语句中比较j==i,因为while循环将temp变量减少为0。

for(i=m;i0)
{
r=温度%10;
j=j+r*r*r;
温度=温度/10;
}
如果(j==i)
{
printf(“%d是armstrong\n”,i);
}
}

在while循环中,您需要使用变量temp使i保持不变,以便for循环正确地继续。在开始新的while循环之前,将j=0设置为从0开始再次计数。在if语句中比较j==i,因为while循环将temp变量减少为0。

尝试将if语句移动到while循环中。在for循环(
i++
)和while循环(
i=i/10;
)中修改
i
)似乎不是一个好主意。这意味着您的for循环不会在从
m
n
的所有值上运行。请尝试将if语句移动到while循环中。在for循环(
i++
)和while循环(
i=i/10;
)中修改
i
)似乎不是一个好主意。这意味着您的for循环不会在从
m
n
的所有值上运行。