|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
代码如下:
#include<stdio.h>
int main()
{
printf("请依次输入整数成绩(输入stop停止):");
float ave;
int times,total;
char scores;
times = 0;
total = 0;
while(scores == 'stop')
{
scanf("%d",&scores);
total += scores;
times+=1;
printf("%d\n",total);
}
ave = (float)total/times;
printf("%.2f",ave);
}
我的想法是每次输入一个分数,然后打印出平均值,输入'stop'就结束输入,打印出平均值,但是尝试了好几次都不行
改成输入0结束代码如下:
#include<stdio.h>
int main()
{
printf("请依次输入整数成绩(输入0停止):");
float ave;
int times,total,scores;
times = 0;
total = 0;
do
{
scanf("%d",&scores);
total = total + scores;
times++;
}while(scores != 0);
ave = (float)total/(times - 1);
printf("%.2f",ave);
}
如果我想改成输入'stop',结束输入的话该怎么改,求助!!!
本帖最后由 jackz007 于 2022-9-24 14:21 编辑
- #include <stdio.h>
- #include <string.h>
- int main(void)
- {
- int times , total , scores ;
- char s[256] ;
- float ave ;
- printf("请依次输入整数成绩(输入stop停止):") ;
- for(total = times = 0 ;;) {
- gets(s) ;
- if(strncmp(s , "stop" , 4)) {
- sscanf(s , "%d" , & scores) ;
- total += scores ;
- times += 1 ;
- printf("%d\n" , total) ;
- } else {
- break ;
- }
- }
- ave = (float) total / times ;
- printf("the average score = %.2f\n" , ave) ;
- }
复制代码
编译、运行实况:
- D:\[00.Exerciese.2022]\C>g++ -o x x.c
- D:\[00.Exerciese.2022]\C>x
- 请依次输入整数成绩(输入stop停止):100
- 100
- 98
- 198
- 95
- 293
- 96
- 389
- 92
- 481
- stop
- the average score = 96.20
- D:\[00.Exerciese.2022]\C>
复制代码
|
|