为什么程序打印0.00

时间:2015-04-17 17:25:48

标签: c

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

  1. 如果废物小于1000kw /小时乘以1.2
  2. 如果浪费在每小时1000到1850千瓦之间,则乘以1.2倍。
  3. 如果废物大于每小时1850kw,则乘以0.9
  4. 我写了这个程序,当我运行它并添加浪费和小时的值时,无论我输入哪个值,费用都会给我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;
    }
    

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

3 个答案:

答案 0 :(得分:7)

if ( ( c = 1000 ) && ( c < 1851 ) ) {

应该是

if ( ( c >= 1000 ) && ( c <= 1850 ) ) {

您可能输入了拼写错误并输入=而不是>

使用时

if( ( c = 1000 ) && ( c < 1851 ) ){

您要将1000分配给c


答案 1 :(得分:3)

您正在使用赋值运算符,而不是检查两个操作数是否相等。变化

if((c=1000) && (c<1851))

if((c==1000) && (c<1851))

另外,第二个条件毫无意义,因为如果c等于1000,{{1}}必须小于1851。

答案 2 :(得分:0)

最好使用&#39; if if else if structure&#39;在多种情况下。 您只使用独立工作的语句。 下面给出了问题的正确代码。

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

int main()
{

    int c; //energy waste//

    float p=0, h; // p=fee h=hours//

    printf("enter energy waste \n");

    scanf("%d",&c);

    printf("enter hours\n");
    scanf("%f",&h);
    if (c<1000){
        p=h*1.2;
    }
    else if((c>=1000)&&(c<1851)){
        p=h*1.2;}
    else if(c>1850){
        p=h*0.9;
    }
    else{
         printf("you enter wrong energy waste data\n");
    }
    printf("Fee: %f", p);
    return 0;
}