编程while循环使用C [英] Programming while loop in C

查看:125
本文介绍了编程while循环使用C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我是一个总的初学者,所以这个问题可能是各位很明显,但我不明白有什么错在这个程序中的while循环。该方案的德目的是计算在用户输入0时,他希望继续投入号码和当他想要停止输入1数之间的平均,所以循环应该停止,当用户把1和来计算值的总和,当他在最后进入0。所以这是我写的,我用 stdio.h中文件stdlib.h 图书馆:

First, I am a total beginner, so the question is probably very obvious for all of you, but i don't get what's wrong with the while loop in this program. Te aim of the program is to calculate the average between numbers where the user inputs 0 when he wants to continue putting numbers in and inputs 1 when he wants to stop, so the loop is supposed to stop when the user puts 1 and to compute a sum of the values when he enters 0 at the end. So this is what i wrote, i used stdio.h and stdlib.h as libraries :

int decision;
int value;
int sum = 0;
float av;
int order = 1;

printf ("for continue press: 0\n ");
printf ("for stopping press: 1\n ");

while (decision == 0) {
    printf("input value:");

    scanf("%d", &value);
    sum = sum + value;

    printf ("continue?");

    scanf("%d", &decision);
    order = order + 1;
}
av = (float) sum / (float) order;

printf("the average is: %.2f", av);
return EXIT_SUCCESS;

只是平均为:0.00什么终端显示器,它跳过上面的整个操作

what the terminal displays is just "the average is:0.00", it skips the whole operation above.

推荐答案

您应该初始化的决定 0

int decision = 0;

这样while循环是真的

so that the while loop is true

while (decision == 0) {

上的第一次迭代。

on the first iteration.

这篇关于编程while循环使用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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