如果该语句为真,如何只显示一条printf语句?

时间:2019-02-11 03:23:10

标签: c variables printf statements

如果包裹重量超过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,但是无论如何它都会出现。

4 个答案:

答案 0 :(得分:4)

您是否尝试放置一些if-else语句?

int main()
{
    double distance, weight, weightCharges, shippingCharge, distanceCharge; // no need for 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 = ;

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

}

答案 1 :(得分:3)

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

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

答案 2 :(得分:3)

int 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 <= 20)
        weightCharges = weight * 5.00;
        else
            if (weight <= 100)
                weightCharges = weight * 10.00;
            else (weight > 100);
        weightCharges= 0;
    if (distance > 1000)
        distanceCharge = weightCharges + 20;
    shippingCharge = weightCharges + distanceCharge;
    unableToShip = weight > 100;

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

}

答案 3 :(得分:1)

首先,html, body { margin: 0; } .container { min-height: 100vh; display: flex; flex-direction: column; } .container > * { padding: .5em; } .a { flex-grow: 6; background-color: #eee; } .b { flex-grow: 4; background-color: #ff0; }的类型应为<div class="container"> <header>header</header> <div class="a">A</div> <div class="b">B</div> <footer>footer</footer> </div>unableToShip(如果使用的是最新且支持int的编译器)。 bool不适用于这种情况。

第二,即使<stdbool.h>是布尔条件的正确类型,作为参数传递给double也不会使unableToShip有条件地工作。如果您有格式字符串可以接受,那么它将成为格式化输出的一部分。您需要准备的是printf,后跟printf语句。

第三,如果要打印的字符串不包含任何格式并以新行结尾,则应使用if (unableToShip)而不是printf