为什么程序打印0.00

-1 c

提出的问题是:"考虑下一个问题,创建一个计算2个月内能源消耗的问题:

  1. 如果废物小于每小时1000千瓦,则乘以1.2
  2. 如果废物在每小时1000到1850千瓦之间,则乘以1.2.
  3. 如果废物大于每小时1850kw,则乘以0.9

我写了这个程序,当我运行它并添加浪费和小时的值时,无论我输入哪个值,费用都会给我0.00.

#include <stdio.h>
#include <math.h>

int main()
{
   int c; //energy waste//
   float p, h; // p=fee h=hours//

   printf("Introduce el consumo y el numero de horas:");
   scanf("%d %f ", &c, &h);

   if (c<1000) {
        p=h*1.2;
   }
   if ((c=1000) && (c<1851)) {
        p=h*1.2;
   }
   if (c>1850) {
        p=h*0.9;
   }

   printf("Fee: %f", p);
   return 0;
}
Run Code Online (Sandbox Code Playgroud)

我需要收取费用.我希望它写得很好,因为我讲西班牙语并且不熟悉英语中的编程概念.

Aru*_*A S 7

if ( ( c = 1000 ) && ( c < 1851 ) ) {
Run Code Online (Sandbox Code Playgroud)

应该

if ( ( c >= 1000 ) && ( c <= 1850 ) ) {
Run Code Online (Sandbox Code Playgroud)

你可能有一个拼写错误而=不是打字>

当你使用

if( ( c = 1000 ) && ( c < 1851 ) ){
Run Code Online (Sandbox Code Playgroud)

你要分配1000c.


  • 我认为应该是`c> = 1000`,否则表达没有多大意义. (2认同)