鱼C论坛

 找回密码
 立即注册
查看: 1824|回复: 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 编辑
#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>
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-9-24 14:18:28 | 显示全部楼层    本楼为最佳答案   
本帖最后由 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>
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

感谢!虽然还有一些看不懂的代码哈哈哈哈哈
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

       主要是输入字符串 scanf() 不可以用 "%d" 的格式描述符。  
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-6-27 22:15

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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