【问题标题】:How do I get the intended output in C programming? [closed]如何在 C 编程中获得预期的输出? [关闭]
【发布时间】:2022-09-27 20:39:52
【问题描述】:

我想编写一个代码,根据衣物的重量计算洗衣费用。我希望它在 4 次输入后停止并计算当天的总销售额。我似乎无法正确打印输出,它继续为所有输出打印 0。

#include<stdio.h>

int main() {
  printf(\"\\t\\tWELCOME TO UNIMAP LAUNDRY\");
  printf(\"\\nThis program is to display laundry price paid by customers\\n\");
  float l, W, sum, r;
  for (l = 0; l < 4; l++) {
    printf(\"Please enter laundry weight(kg):\");
    scanf(\"%f\", & W);
    printf(\"Total amount to pay is RM%f\\n\", r);
    if (W < 1)
      r == (W * 1.2);
    else if (W < 7)
      r == (W * 0.9);
    else if (W < 12)
      r == (W * 0.6);
    else
      r == (W * 0.7);
  }
  sum += r;
  printf(\"The total sales are RM%f\", sum);
  return 0;
}

我得到的输出示例:

WELCOME TO UNIMAP LAUNDRY
This program is to display laundry prices paid by customers
Please enter laundry weight(kg):12
The total amount to pay is RM0.000000
Please enter laundry weight(kg):6
The total amount to pay is RM0.000000
Please enter laundry weight(kg):5
The total amount to pay is RM0.000000
Please enter laundry weight(kg):9
The total amount to pay is RM0.000000
The total sales are RM-29726079709203136512.000000
  • 请缩进您的代码以使其可读。
  • 帮自己一个忙,正确缩进你的代码。使用不明代码非常困难,即使对于顶级程序员来说,更不用说初学者了。还要使用有意义的变量名,而不是 Wl 等。
  • 您可能希望将打印“Total amount to pay”的行移到循环末尾; sum 变量应该被初始化,sum=0,并且sum += r; 行也应该被移动到循环内。

标签: c


【解决方案1】:

== 是一个布尔运算符, = 是分配。

所以而不是
r == (W * 1.2);

r = (W * 1.2);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-18
    • 2021-08-31
    • 2021-02-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多