只需1输入。需要很多输入

时间:2014-08-02 10:54:31

标签: c

我刚刚开始在C中学习编程,这很有趣。我想写一个程序,这样我就可以让我的学校更轻松。

我知道这非常低效。有效率的提示对我有帮助,但我的主要问题是当我想输入选项3来计算东西时,我必须在终端窗口上按3次才能开始执行。

我的问题是我只想输入一次并直接进入它。

这是代码。你应该只能阅读4行,并了解正在发生的事情。

#include <stdio.h>
#define R 8.31
/* Using the PV = nRT formula */

int

main(int argc, char *argv[]) {

    double Temperature, Pressure, Mols, Volume, Variable;
    printf("Type the number of the variable are you finding:\n 1: Temperature\n 2: Pressure\n 3: Volume\n 4: Mols\n");
    if (scanf("%lf",&Variable) == 1 && (Variable - 1 == 0)) {
        printf("Enter your Pressure in Pa: \n");
        scanf("%lf", &Pressure);
        printf("Enter your Volume in L: \n");
        scanf("%lf", &Volume);
        printf("Enter your Mols in mol: \n");
        scanf("%lf", &Mols);
        Temperature = (Pressure * Volume) / (Mols * R);
        printf("The Temperature is %.3f K (Kelvin)\n", Temperature);

    }   
    else if(scanf("%lf", &Variable) == 1 && (Variable - 2 == 0)){       
        printf("Enter your Temperature in K: \n");
        scanf("%lf", &Temperature);
        printf("Enter your Volume in L: \n");
        scanf("%lf", &Volume);
        printf("Enter your Mols in mol: \n");
        scanf("%lf",&Mols);
        Pressure = (Mols * R * Temperature)/Volume;
        printf("The Pressure is %.3f Pa (Pascals)\n", Pressure);

        }  
    else if(scanf("%lf", &Variable) == 1 && (Variable - 3 == 0)){       
        printf("Enter your Temperature in K: \n");
        scanf("%lf", &Temperature);
        printf("Enter your Pressure in Pa: \n");
        scanf("%lf", &Pressure);
        printf("Enter your Mols in mol: \n");
        scanf("%lf",&Mols);
        Volume = (Mols * R * Temperature)/Pressure;
        printf("The Volume is %.3f L (Litres)\n", Volume);

        }  
    else if(scanf("%lf", &Variable) == 1 && (Variable - 4 == 0)){       
        printf("Enter your Temperature in K: \n");
        scanf("%lf", &Temperature);
        printf("Enter your Volume in L: \n");
        scanf("%lf", &Volume);
        printf("Enter your Pressure in Pa: \n");
        scanf("%lf",&Pressure);
        Mols = (Pressure * Volume)/(R * Temperature);
        printf("The Amount of mols is %.3f mols \n", Mols);

        }  

    else {
        printf("Invalid Input, please try again\n");
    }

    return 0;

}

3 个答案:

答案 0 :(得分:3)

问题是您每次都要阅读并测试变量。读取变量一次,然后多次测试。这样的事情:( switch不是必需的,您可以使用if else构造)

int Variable;

if (scanf("%d",&Variable) != 1) {
 // error handling
}


switch (Variable) {
  case 1:
    // code
    break;
  case 2:
    // code
    break;
  case 3:
    // code
    break;
  case 4:
    // code
    break;
  default:
    printf("Invalid Input\n");
}

加分:

  • 当整数足够时,不要使用浮点数。浮点数有精确的问题,因为你肯定会找到路。在您的示例中,Variable只能1234,您一定要int(无论您使用的是switch需要整数数据类型的if-else或使用浮点运算的variable - 4 == 0。)
  • 变量名称的大写不常见(我不是说不使用这个约定,我只是让你意识到这个事实)
  • 支持代码的清晰度,例如:请勿使用variable == 4,请使用{{1}}

答案 1 :(得分:0)

您可以使用switch和case语句来满足您的需求。在上面的代码中,无法直接进入特定操作。如果是,则使用scanf,因此用户需要多次输入输入以选择特定操作。假设,如果你想进行第4次操作&amp;输入是4,4,4,4。

答案 2 :(得分:0)

Scanf需要输入。程序顺序首先检查if输入是否为if,如果为false则再次需要输入。所以你需要输入3次才能执行第三次elseif。更好的做法是为变量使用整数值。我推荐切换而不是所有这些。

相关问题