鱼C论坛

 找回密码
 立即注册
查看: 2964|回复: 3

[已解决]代码可以这样写吗

[复制链接]
发表于 2022-10-30 09:09:32 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include<stdio.h>
int main()
{        int i,sum,n;       
        n = 100;
        sum = 0;
        for (i = 1; i<=n; i++)
          sum = sum+i;
        printf("sum of number from 1 to %d is %d\n",n,sum);
        int a,b,c;
        a = 1;
        b = 0;
        c = 100;
        while(a<=c)
        {        b = b+1;
                a = a++;
        }
        printf("n of number from 1 to %d is %d\n",c,b);
        return 0;
}
想象中的运行结果
sum of number from 1 to 100 is 5050
n of number from 1 to 100 is 5050
实际上
sum of number from 1 to 100 is 5050

有什么办法可以不让它从中间就跳出运行吗
最佳答案
2022-10-30 09:13:42
你这个 while 循环成死循环了
a = a++; 这一句 应该改为 a = a + 1; 或 a++;
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-10-30 09:13:42 | 显示全部楼层    本楼为最佳答案   
你这个 while 循环成死循环了
a = a++; 这一句 应该改为 a = a + 1; 或 a++;
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-10-30 09:20:44 | 显示全部楼层
2楼正解,不过即使修改后也看不到
n of number from 1 to 100 is 5050
而是
n of number from 1 to 100 is 100

另外打开编译警告看看吧,能减少不少问题
test.c:15:22: warning: multiple unsequenced modifications to 'a' [-Wunsequenced]
                a = a++;
                  ~  ^
1 warning generated.
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-10-30 15:03:20 | 显示全部楼层
哦吼,谢谢啦,我也发现了我while 语句那里b = b+1 应该改为b = b+a 才能看到我想要的结果
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-6-18 05:25

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表