|
发表于 2020-10-27 18:38:52
From FishC Mobile
|
显示全部楼层
|阅读模式
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
求问各位大佬,我这代码哪里错了,为什么最后输出结果时e那个公式可以运行成功而m那个公式运行不成功???
#include<stdio.h>
int main()
{
int m,e,choice1,choice2;
double a,cost;
printf("Enter a:");
scanf("%lf",&a);
printf("[90]seclet 90\n");
printf("[93]select 93\n");
printf("[97]select 97\n");
printf("Enter choice1:");
scanf("%d",&choice1);
printf("[m]select m\n");
printf("[e]select e\n");
printf("Enter choice2:");
scanf("%d",&choice2);
switch(choice1){
case 90:cost=6.95;break;
case 93:cost=7.44;break;
case 97:cost=7.93;break;
default:cost=0.00;break;
}
if(choice2==m){
cost=cost*a*(1-0.05);}
else if(choice2==e){
cost=cost*a*(1-0.03);}
else
printf("default\n");
printf("%.2f\n",cost);
return 0;
}
刚下班回来,又仔细查看了我前面发的程序,确实存在错误没有修改: 1、choice2 应该是 char 型;2、choice1 输入后,没有吸收回车,导致后面输入字符时,总是将这个回车赋给了 choice2;3、输入 choice2 时,使用的控制符不正确;4、比较字符时,要加单引号
- #include<stdio.h>
- int main()
- {
- int m,e,choice1;
- char choice2; // 由于接收的是字符
- double a,price,cost;
- printf("Enter how much gas a = ");
- scanf("%lf",&a);
- printf("90# gas seclet 90\n");
- printf("93# gas select 93\n");
- printf("97# gas select 97\n");
- printf("Enter choice1: ");
- scanf("%d",&choice1);
- getchar(); //吸收回车键, 此语句最为关键
-
- switch(choice1)
- {
- case 90:price=6.95 * a;break;
- case 93:price=7.44 * a;break;
- case 97:price=7.93 * a;break;
- default:price=0.00;break;
- }
- printf("%.2f\n",price); // 没有优惠时该付金额
-
- printf("self-service select m\n");
- printf("ancillary-service select e\n");
- printf("Enter choice2: ");
- scanf("%c",&choice2); // 接收字符,用了 %d
- getchar(); // 吸收回车键,你的错误主要在此,你输入的是字符,回车也是字符哟~~
-
- if(choice2 == 'm') // 单引号不能少
- {
- price = (1 - 0.05) * price;
- }
- else if(choice2 == 'e') // 单引号不能少
- {
- price = (1 - 0.03) * price;
- }
- printf("\n");
- printf("You need pay %.2lf yuan\n",price);
- return 0;
- }
复制代码
|
|