鱼C论坛

 找回密码
 立即注册
查看: 2564|回复: 4

[已解决]求解,不知道哪里有错误

[复制链接]
发表于 2022-9-24 14:08:39 | 显示全部楼层 |阅读模式

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

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

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',结束输入的话该怎么改,求助!!!
最佳答案
2022-9-24 14:18:28
本帖最后由 jackz007 于 2022-9-24 14:21 编辑
  1. #include <stdio.h>
  2. #include <string.h>

  3. int main(void)
  4. {
  5.         int times , total , scores                    ;
  6.         char s[256]                                   ;
  7.         float ave                                     ;
  8.         printf("请依次输入整数成绩(输入stop停止):") ;
  9.         for(total = times = 0 ;;) {
  10.                 gets(s)                               ;
  11.                 if(strncmp(s , "stop" , 4)) {
  12.                         sscanf(s , "%d" , & scores)   ;
  13.                         total += scores               ;
  14.                         times += 1                    ;
  15.                         printf("%d\n" , total)        ;
  16.                 } else {
  17.                         break                         ;
  18.                 }
  19.         }
  20.         ave = (float) total / times                   ;
  21.         printf("the average score = %.2f\n" , ave)    ;
  22. }
复制代码

        编译、运行实况:
  1. D:\[00.Exerciese.2022]\C>g++ -o x x.c

  2. D:\[00.Exerciese.2022]\C>x
  3. 请依次输入整数成绩(输入stop停止):100
  4. 100
  5. 98
  6. 198
  7. 95
  8. 293
  9. 96
  10. 389
  11. 92
  12. 481
  13. stop
  14. the average score = 96.20

  15. D:\[00.Exerciese.2022]\C>
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-9-24 14:18:28 | 显示全部楼层    本楼为最佳答案   
本帖最后由 jackz007 于 2022-9-24 14:21 编辑
  1. #include <stdio.h>
  2. #include <string.h>

  3. int main(void)
  4. {
  5.         int times , total , scores                    ;
  6.         char s[256]                                   ;
  7.         float ave                                     ;
  8.         printf("请依次输入整数成绩(输入stop停止):") ;
  9.         for(total = times = 0 ;;) {
  10.                 gets(s)                               ;
  11.                 if(strncmp(s , "stop" , 4)) {
  12.                         sscanf(s , "%d" , & scores)   ;
  13.                         total += scores               ;
  14.                         times += 1                    ;
  15.                         printf("%d\n" , total)        ;
  16.                 } else {
  17.                         break                         ;
  18.                 }
  19.         }
  20.         ave = (float) total / times                   ;
  21.         printf("the average score = %.2f\n" , ave)    ;
  22. }
复制代码

        编译、运行实况:
  1. D:\[00.Exerciese.2022]\C>g++ -o x x.c

  2. D:\[00.Exerciese.2022]\C>x
  3. 请依次输入整数成绩(输入stop停止):100
  4. 100
  5. 98
  6. 198
  7. 95
  8. 293
  9. 96
  10. 389
  11. 92
  12. 481
  13. stop
  14. the average score = 96.20

  15. D:\[00.Exerciese.2022]\C>
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-9-24 14:26:00 | 显示全部楼层
jackz007 发表于 2022-9-24 14:18
编译、运行实况:

感谢!虽然还有一些看不懂的代码哈哈哈哈哈
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-9-24 14:27:57 | 显示全部楼层
灯火阑珊201 发表于 2022-9-24 14:26
感谢!虽然还有一些看不懂的代码哈哈哈哈哈

       主要是输入字符串 scanf() 不可以用 "%d" 的格式描述符。  
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-9-24 14:28:51 | 显示全部楼层
问题在于:根据数据类型要求,%d读入的是数字,你要求在读入stop的时候停止,数据类型不符合。应该会出错。读入单个字符是用%c,读入字符串是用%s。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-16 09:20

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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