MODE ——计算了 任意多个数字的平均值(知识点:for的循环)
生活随笔
收集整理的這篇文章主要介紹了
MODE ——计算了 任意多个数字的平均值(知识点:for的循环)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
問題描述:
輸入浮點數值 判斷是否繼續輸入 輸出N時候 退出for循環 計算出來 輸入所有數字的平均值
運行結果:
[root@J01051386 C]# ./a.out This program calculates the average of any number of values. Enter a value:1 Do you want to enter another value?(Y or N):yEnter a value:2 Do you want to enter another value?(Y or N):yEnter a value:3 Do you want to enter another value?(Y or N):nThe average is 2.00代碼部分:
#include <stdio.h> #include <ctype.h> int main(void) {char answer = 'N';double total = 0.0;double value = 0.0;unsigned int count = 0;printf("\nThis program calculates the average of any number of values."); //for 循環中的break 的使用 for (;;){ printf("\nEnter a value:");scanf(" %lf",&value);total += value;++count;printf("Do you want to enter another value?(Y or N):");scanf(" %c",&answer);if(tolower(answer) == 'n')break;} printf("\nThe average is %.2lf\n",total/count);return 0; }?
總結
以上是生活随笔為你收集整理的MODE ——计算了 任意多个数字的平均值(知识点:for的循环)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C 判断 —— switch语句(多个s
- 下一篇: C 预处理器 —— __DATE__ #