计算快递费的代码不能跑,不知道为什么,求助大佬
#include <stdio.h>
int main()
{
float weight,total;
int kind,way;
printf("请输入快递的种类(1.市内、2.省内、3.省外): ");
scanf("%d",kind);
while(1)
{
if(kind<1||kind>3)
{
printf("请在提供的服务中选择:");
scanf("%d",kind);
}
else
break;
}
printf("\n");
printf("请输入快递的方式(1.普通快递、2.特快专递):");
scanf("%d",way);
while(1)
{
if(way<0||way>2)
{
printf("请在提供的服务中选择:");
scanf("%d",way);
}
else
break;
}
printf("\n");
printf("请输入快递的重量:");
scanf("%f",weight);
while(1)
{
if(weight<=0)
{
printf("请输入正确的重量:");
scanf("%f",weight);
}
else
break;
}
if(kind==1)
{
if(way==1)
{
if(weight<=1)
total==6;
else
total=6+1*(weight-1);
printf("快递费用为%f元",total);
}
else
{
if(weight<=1)
total==12;
else
total=12+2*(weight-1);
printf("快递费用为%f",total);
}
}
if(kind==2)
{
if(way==1)
{
if(weight<=1)
total==6;
else
total=6+1.5*(weight-1);
printf("快递费用为%f",total);
}
else
{
if(weight<=1)
total==13;
else
total=13+2*(weight-1);
printf("快递费用为%f",total);
}
}
if(kind==3)
{
if(way==1)
{
if(weight<=1)
total=10;
else
total=10+3*(total-1);
printf("快递费用为%f",total);
}
else
{
if(weight<=1)
total=22;
else
total=22+10*(total-1);
printf("快递费用为%f",total);
}
}
return 0;
}
这是运行的结果
本帖最后由 jackz007 于 2021-11-5 02:15 编辑
关注带有 scanf() 的语句,因为,所有对 scanf() 调用的写法都是错误的。
下面是我修改的代码,谨供楼主参考。
#include <stdio.h>
int main(void)
{
float weight,total ;
int kind , way ;
while(1)
{
printf("请输入快递的种类(1.市内、2.省内、3.省外): ") ;
scanf("%d", & kind) ;
if(kind > 0 && kind < 4) break ;
else printf("选择无效!\n\n") ;
}
while(1)
{
printf("请输入快递的方式(1.普通快递、2.特快专递): ") ;
scanf("%d", & way) ;
if(way > 0 && way < 3) break ;
else printf("选择无效!\n\n") ;
}
while(1)
{
printf("请输入快递的重量 : ") ;
scanf("%f" , & weight) ;
if(weight > 0) break ;
else printf("重量无效!\n\n") ;
}
total = 0 ;
if(kind > 0 && kind < 4 && way > 0 && way < 3 && weight > 0)
{
if(kind == 1)
{
if(way==1)
{
if(weight <= 1) total = 6 ;
else total = 6 + 1 * (weight - 1) ;
}
else
{
if(weight <= 1) total = 12 ;
else total= 12 + 2 * (weight - 1) ;
}
}
else if(kind == 2)
{
if(way==1)
{
if(weight <= 1) total = 6 ;
else total = 6 + 1.5 * (weight - 1) ;
}
else
{
if(weight <= 1) total = 13 ;
else total = 13 + 2 * (weight - 1) ;
}
}
else
{
if(way==1)
{
if(weight <= 1) total = 10 ;
else total = 10 + 3 * (total - 1) ;
}
else
{
if(weight <= 1) total = 22 ;
else total = 22 + 10 * (total - 1) ;
}
}
printf("快递费用为: %f\n" , total) ;
}
return 0;
} {:10_243:} jackz007 发表于 2021-11-5 02:08
关注带有 scanf() 的语句,因为,所有对 scanf() 调用的写法都是错误的。
下面是我修改的 ...
修改后的代码是可以的,谢谢,看来我得去翻看一下scanf函数,不过这里好像没有设置为最佳答案的选项,不知道是哪里出了问题
心驰神往 发表于 2021-11-5 08:02
好家伙,水贴
jackz007 发表于 2021-11-5 02:08
关注带有 scanf() 的语句,因为,所有对 scanf() 调用的写法都是错误的。
下面是我修改的 ...
技术 交流区是不是没有设置为最佳答案选项的?
我来看看 看看代码
湫的小草原 发表于 2021-11-5 08:14
好家伙,水贴
发求助帖才行 湫的小草原 发表于 2021-11-5 08:15
技术 交流区是不是没有设置为最佳答案选项的?
没有
问题求助才有 嘉岳呀 发表于 2021-11-5 19:34
没有
问题求助才有
是啊,我之前不知道
页:
[1]