yyyyjk1031
2020-10-10 11:28
采纳率: 50%
浏览 357

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);

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题