m0_58683963 2022-05-05 11:09 采纳率: 100%
浏览 51
已结题

c语言 while循环在不符合循环条件时循环了

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main() {
double a;
int x,z,b;
scanf("%d",&x);
z = x;
a = 1;
b = 0;
while (x != -1){
a += 1;
scanf("%d",&x);
z += x;
printf("a=%f,z=%d,b=%d",a,z,b);
}
b = z / a;
printf("%f\n",1.0*z/a);

}

这是运行结果
2
3
a=2.000000,z=5,b=04
a=3.000000,z=9,b=05
a=4.000000,z=14,b=0-1
a=5.000000,z=13,b=02.600000


Process exited after 38 seconds with return value 9
请按任意键继续. . .

  • 写回答

2条回答 默认 最新

  • 柒号华仔 2022年度博客之星评选前沿技术领域TOP 1 2022-05-05 11:25
    关注

    代码:

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    /* run this program using the console pauser or add your own getch, system("pause") or input loop */
    
    int main() {
        double b;
        int x,z,a;
        x = 0;
        z = 0;
        a = 0;
        while (x != -1){
            a += 1;
            scanf("%d",&x);
            z += x;
        }
        b = (z*1.0+1) / (a*1.0-1);
        printf("%f",b);
    }
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 5月14日
  • 已采纳回答 5月6日
  • 创建了问题 5月5日

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算