#include"stdio.h"
int main()
{
float x,money,fast;
int t;
printf("whether need fast?number:1 for yes or 0 for no\n");
scanf("%f",&fast);
if(fast==1)
{ printf("please input the weight x=");
scanf("%f",&x);
if(x<=0)
money=0;
else if(x<=1)
money=2.8;
else
{t=x;if(x-t!=0) x=t+1;
else x=t;
money=0.8+(x-1)*0.5+2; }
printf("%f",money);
}
else
{ printf("please input the weight x=");
scanf("%f",&x);
if(x<=0)
money=0;
else if(x<=1)
money=0.8;
else
{ t=x;if(x-t!=0) x=t+1;
else x=t;
money=0.8+(x-1)*0.5; }
printf("%f",money);
}
getch();
return 0;
}
祝你成功
两个毛病
其一在于fast=1
刚学C容易分不清=和==
==是判断是否相等
=是赋值
fast=1会使得fast直接等于1,而1是正确的,就通过。
也就是说不管你输什么,总是认为快。
其二在于
scanf("%d",&x);
使用%d来接受一个float数,会截断,可能有错。而在这就的确出错了。
对待C中的自动转型还是谨慎一点好。
if(fast=1) => if(fast==1)
#include"stdio.h"
int
main()
{
float
x,money,fast;
int
t;
printf("whether
need
fast?number:1
for
yes
or
0
for
no\n");
scanf("%f",&fast);
if(fast==1)
{
printf("please
input
the
weight
x=");
scanf("%f",&x);
if(x<=0)
money=0;
else
if(x<=1)
money=2.8;
else
{t=x;if(x-t!=0)
x=t+1;
else
x=t;
money=0.8+(x-1)*0.5+2;
}
printf("%f",money);
}
else
{
printf("please
input
the
weight
x=");
scanf("%f",&x);
if(x<=0)
money=0;
else
if(x<=1)
money=0.8;
else
{
t=x;if(x-t!=0)
x=t+1;
else
x=t;
money=0.8+(x-1)*0.5;
}
printf("%f",money);
}
getch();
return
0;
}
祝你成功