c语言编写问题10

来源:百度知道 编辑:UC知道 时间:2024/06/30 06:05:10
#include <stdio.h>

void main()
{
char ch;
float cost1,cost2,cost3,total;
double discount,pay;
printf("\n该顾客是否会员?(y/n)");
fflush(stdin);
scanf("%c",&ch);

printf("\n请输入第一种商品的价格:");
scanf("%f",&cost1);
printf("\n请输入第二种商品的价格:");
scanf("%f",&cost2);
printf("\n请输入第三种商品的价格:");
scanf("%f",&cost3);

discount=0;
total=cost1+cost2+cost3;
if((ch=='y')||(total>=100))
{
discount=total*0.1;
}
if((ch=='y')&&(total>=100))
{
discount=total*0.2;
}
pay=total-discount;
printf("\n购物总额= %.2f\n",total);
printf("\n折扣金额= %.2f\n",discount);
printf("\n应付总额= %.2f\n",pay);
}

问题:当把两个if的条件顺序互换时,即((ch=='y')||(total>=100))和((ch==&

是这样的,互换后。因为满足((ch=='y')&&(total>=100)),所以执行了discount=total*0.2; 打八折。

但是,程序继续运行,因为同样也满足((ch=='y')||(total>=100)),所以执行了discount=total*0.1; 所以又打了9折啦。

在你这个问题上可以是这样,在其他问题上你要具体问题具体分析。不能都这样考虑。