gpt4 book ai didi

c - 如果语句为真,如何只显示 printf 语句?

转载 作者:太空宇宙 更新时间:2023-11-04 05:06:28 24 4
gpt4 key购买 nike

我正在做一个运费计算器,如果包裹重量超过 50 磅则无法运送包裹。我想只在包裹重量超过 50 磅时才显示声明,但无论如何它都会显示。

我把它当作 else 语句、if 语句和 if else 语句来试过。

main(){
double distance, weight, weightCharges, shippingCharge, distanceCharge, unableToShip;
printf ("Enter the weight of the package:\n");
scanf ("%lf", &weight);
printf ("Enter the distance your package needs to go: \n");
scanf ("%lf", &distance);

if (weight <= 10)
weightCharges = weight * 3.00;
else
if (weight <= 50)
weightCharges = weight * 5.00;
else (weight > 50);
weightCharges= 0;
if (distance > 1000)
distanceCharge = weightCharges + 10;
shippingCharge = weightCharges + distanceCharge;
unableToShip = weight > 50;

printf ("Your total cost is: %.2lf \n", shippingCharge);
printf ("We're unable to ship your package \n", unableToShip);

}

我希望第二个 printf 仅在我们无法运送他们的包裹时出现,但无论如何它都会出现。

最佳答案

if 在这里的工作方式与在其他任何地方的工作方式相同:

if (unableToShip) 
printf ("We're unable to ship your package\n");

关于c - 如果语句为真,如何只显示 printf 语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54623551/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com