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 等差数列中的素数个数
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证