yyyyjk1031 2020-10-10 11:28 采纳率: 0%
浏览 414
已采纳

c语言if语句不能正常运行?

我的是学生作业,一共有三个小问题。问题出在了第三个问题。第一个和第二问题
都是按着顺序执行,执行完第二个问题以后会直接执行第三个问题的最后一个
else,然后直接结束了。但是单独把问题三拿出来运行是没有问题的。请各位大神帮我看一下是那里写错了呢?谢谢各位大佬。

#include<stdio.h>

int main()

{
char one_sport;
int two_people;
int two_priceA;
int two_priceB;
int two_priceC;
char three_sort;
int three_quantity;
int three_aq;
int three_bq;
int three_cq;
double priceA;
double priceB;
double priceC;

   //problem 1
printf("Problem 1\n");
printf("Enter w for walk and s for swim\n");
scanf("%c",&one_sport);

if(one_sport == 'w')
{
    printf("You need to walk 15.2 hours to burn off a pound\n");
    }   
else
{
    printf("You need to swim 8.75 hours to burn off a pound\n");
}

    //problem 2
    printf("problem 2\n");
    printf("How many in your group\n");
    scanf("%d",&two_people);
    two_priceA = two_people * 15;
    two_priceB = two_people * 12;
    two_priceC = two_people * 10;
    if(two_people >=1&&two_people<=8 )
    {
        printf("Total cost for your group is %d\n",two_priceA); 
    }

    else if(two_people >=9&&two_people<=15)
    {
        printf("Total cost for your group is %d\n",two_priceB); 
    }

    else 
    {
        printf("Total cost for your group is %d\n",two_priceC); 
    }


    //problem 3

    printf("problem 3\n");
    printf("Enter A if you want to purchase a shirt,a B if you want to purchase a hat,\nand C if you want to enter a poster.\n");

    scanf("%c",&three_sort);


    switch(three_sort)
    {
        case 'A':printf("Enter your quantity\n"); break;
        case 'B':printf("Enter your quantity\n"); break;
        case 'C':printf("Enter your quantity\n"); break;

    }

    if(three_sort =='A')
    {

        scanf("%d",&three_aq);
        priceA = three_aq * 10;
        printf("You owe $%.2lf\n",priceA);

    }
    else if(three_sort =='B')
    {

        scanf("%d",&three_bq);
        priceB = three_bq * 20;
        printf("You owe $%.2lf\n",priceB);

    }
    else if(three_sort =='C')
    {

        scanf("%d",&three_cq);
        priceC = three_cq * 30;
        printf("You owe $%.2lf\n",priceC);

    }

    else
    {
        printf("Your owe $0");
    }
}
  • 写回答

1条回答 默认 最新

  • threenewbee 2020-10-10 12:09
    关注

    scanf("%c",&three_sort);
    的前面加上一个
    fflush(stdin);

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛