为什么这不能正确执行?为什么不从用户那里获取rpm的价值呢?

时间:2018-03-08 17:55:04

标签: c

#include<stdio.h>
void main() {

    float x;
    printf("Temperature = %f", x);
    scanf("%f \n", &x);

    int y;
    printf("Sensor Reading = %d ", y);
    scanf("%d \n", &y);

    int z;
    printf("RPM Value = %d", z);
    scanf("%d", &z);
    if((x<=143.4)&&(y=0)) {
        printf("Continue to run\n");
        printf("Temperature is Low\n");
        printf("Not mixed correctly\n");
        if(z<400) {
            printf("Increase the speed to 400rpm");
        } else if(z>400) {
            printf("Decrease the speed to 400rpm");
        } else {
            printf("keep running at this speed");
        }
    } else {
        printf("stop running\n");
        printf("Temperature is above 143.4 degree Celcius\n");
        printf("Colors are mixed correctly\n");
    }
}

这是我在下面给出的问题的代码吗?

化学混合物在织物着色设备中运行。如果混合物的温度升至143.4℃以上,则应停止混合。混合物内部有一个传感器,用于检查颜色是否与预期颜色混合,如果颜色正确,传感器将发送信号1,否则为0。如果颜色混合正确,则需要混合物停止混合。如果混合动力马达应以400rpm的速度运转,如果速度较低,则必须提高速度或降低速度。编写程序以打印混合物的状态(是否继续运行,停止,增加/减少速度)以及停止原因等详细信息。接受用户输入作为温度,传感器读数和RPM值,并检查不同的输入。

2 个答案:

答案 0 :(得分:0)

你的代码运行不正常的原因是你在为变量赋值之前打印值,而在条件你为y赋值(通过做y = 0)而不是比较值(比如y == 0)

尝试以下代码。

#include<stdio.h>
void main() {

    float x;
    printf("Enter Temperature: ");
    scanf("%f", &x);


    int y;
    printf("Enter Sensor Reading: ");
    scanf("%d", &y);


    int z;
    printf("Enter RPM Value: ");
    scanf("%d", &z);
    printf("\nTemperature = %f\n", x);
    printf("Sensor Reading = %d\n", y);
    printf("RPM Value = %d\n\n", z);

    if( x <= 143.4 && y == 0 ) 
    {
        printf("Continue to run\n");
        printf("Temperature is Low\n");
        printf("Not mixed correctly\n");
        if( z < 400 ) 
        {
            printf("Increase the speed to 400rpm\n");
        } 
        else if( z > 400 ) 
        {
            printf("Decrease the speed to 400rpm\n");
        } 
        else 
        {
            printf("keep running at this speed\n");
        }
    } 
    else 
    {
        printf("stop running\n");
        if( x > 143.4 )
        {
            printf("Temperature is above 143.4 degree Celcius\n");
        }
        if( y == 1 )
        {
            printf("Colors are mixed correctly\n");
        }
    }
}

答案 1 :(得分:0)

在打印之前询问数据是有意义的。

这是将y设为0y=0    这是检查y是否相等0y==0

看看:

#include <stdio.h>
#include <stdlib.h>


void main(void) {

    float x;

    printf("Set Temperature:\n"); 

    scanf("%f", &x);
    printf("Temperature = %.3f\n", x);


    int y;

    printf("Set Sensor Reading:\n"); 
    scanf("%d", &y);    
    printf("Sensor Reading = %d \n", y);


    int z;

    printf("Set RPM Value:\n", z);
    scanf("%d", &z);
    printf("RPM Value = %d\n", z);


    if( (x<=143.4) && (y==0) ) 
    {
        printf("Continue to run\n");
        printf("Temperature is Low\n");
        printf("Not mixed correctly\n");

        if (z<400) {
            printf("Increase the speed to 400rpm\n");
        } 
        else 
            if(z>400)
            {
                printf("Decrease the speed to 400rpm\n");
            } 
            else
            {
                printf("keep running at this speed\n");
            }
    }
    else
    {
        printf("stop running\n");
        printf("Temperature is above 143.4 degree Celcius\n");
        printf("Colors are mixed correctly\n");
    }
    return 0;
}

输出:

Set Temperature:                                                                                                                               
143.4                                                                                                                                          
Temperature = 143.400                                                                                                                          
Set Sensor Reading:                                                                                                                            
20                                                                                                                                             
Sensor Reading = 20                                                                                                                            
Set RPM Value:                                                                                                                                 
10                                                                                                                                             
RPM Value = 10                                                                                                                                 
stop running                                                                                                                                   
Temperature is above 143.4 degree Celcius                                                                                                      
Colors are mixed correctly