鱼C论坛

 找回密码
 立即注册
查看: 1276|回复: 2

[已解决]课后作业统计元音字母的问题

[复制链接]
发表于 2018-9-8 03:55:39 | 显示全部楼层 |阅读模式

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

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

x
#include <stdio.h>

int main()
{
        char ch;
        int count = 0;
        int a = 0, e = 0, i = 0, o = 0, u = 0;

        printf("Please input your sentence:");

        while((ch = getchar()) != '\n')
        {
                if(ch = 'a')
                {
                        a++;
                        count++;
                }
                else if(ch = 'e')
                {
                        e++;
                        count++;
                }
                else if(ch = 'i')
{
                        i++;
                        count++;
                }
                else if(ch = 'o')
                {
                        o++;
                        count++;
                }
                else if(ch = 'u')
                {
                        u++;
                        count++;
                }
                else
                        continue;

        }

        printf("There are %d vowels in this sentence\n", count);
        printf("a(%d),e(%d),i(%d),o(%d),u(%d)\n",a,e,i,o,u);
        return 0;
}

程序里面没用答案的switch。。。 目标是统计小写元音字母,但是统计结果是所有输入的字符,请大神帮忙看看是什么问题。。
最佳答案
2018-9-8 09:36:04
你好  在if() 里面的比较中  用==  而不是=
#include <stdio.h>

int main()
{
        char ch;
        int count = 0;
        int a = 0, e = 0, i = 0, o = 0, u = 0;
        printf("Please input your sentence:");
        while((ch = getchar()) != '\n')
        {
                if(ch == 'a')
                {
                        a++;
                        count++;
                }
                else if(ch == 'e')
                {
                        e++;
                        count++;
                }
                else if(ch == 'i')
{
                        i++;
                        count++;
                }
                else if(ch == 'o')
                {
                        o++;
                        count++;
                }
                else if(ch == 'u')
                {
                        u++;
                        count++;
                }
                else
                        continue;

        }

        printf("There are %d vowels in this sentence\n", count);
        printf("a(%d),e(%d),i(%d),o(%d),u(%d)\n",a,e,i,o,u);
        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-9-8 09:36:04 | 显示全部楼层    本楼为最佳答案   
你好  在if() 里面的比较中  用==  而不是=
#include <stdio.h>

int main()
{
        char ch;
        int count = 0;
        int a = 0, e = 0, i = 0, o = 0, u = 0;
        printf("Please input your sentence:");
        while((ch = getchar()) != '\n')
        {
                if(ch == 'a')
                {
                        a++;
                        count++;
                }
                else if(ch == 'e')
                {
                        e++;
                        count++;
                }
                else if(ch == 'i')
{
                        i++;
                        count++;
                }
                else if(ch == 'o')
                {
                        o++;
                        count++;
                }
                else if(ch == 'u')
                {
                        u++;
                        count++;
                }
                else
                        continue;

        }

        printf("There are %d vowels in this sentence\n", count);
        printf("a(%d),e(%d),i(%d),o(%d),u(%d)\n",a,e,i,o,u);
        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-9-9 07:53:24 | 显示全部楼层
gpf谦默 发表于 2018-9-8 09:36
你好  在if() 里面的比较中  用==  而不是=
#include

哈哈,没注意到这个。。谢谢啦!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-10-2 12:37

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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