我有一个我需要编写的C类程序。程序要求数量,我需要将该数量乘以用户输入的另一个变量。 c类的基本计算器脚本:)

我有这样的设置,

    int qty; //basic quantity var
float euro, euro_result;

//assign values to my float vars
euro = .6896; //Euro Dollars
    euro_result = euro * qty; // Euro Dollars multiplied by user input qty

//start program for user
printf("Enter a quantity: ");

//alow user to input a quantity
scanf("%d", &qty);

printf("Euro:       %f \n", euro_result);

为什么它不能按预期工作?

有帮助吗?

解决方案

在用户输入之前,您已将欧元乘以用户给定数量qty。 它应该如下:     // euro_result = euro * qty; // <!> lt; - 将其移至下面给出的位置

//start program for user
printf("Enter a quantity: ");

//alow user to input a quantity
scanf("%d", &qty);

euro_result = euro * qty; // Euro Dollars multiplied by user input qty

printf("Euro:       %f \n", euro_result);

多数人。

其他提示

错误是该行

euro_result = euro * qty;

需要在数量读入后

C程序中的语句按顺序执行,表达式不符号计算。因此,您需要以这种方式重新排序语句:

int qty;
float euro, euro_result;

euro = .6896; // store constant value in 'euro'

printf("Enter a quantity: ");

scanf("%d", &qty); // store user input in 'qty'

euro_result = euro * qty; // load values from 'euro' and 'qty',
                          // multiply them and store the result
                          // in 'euro_result'

printf("Euro:       %f \n", euro_result);

我怀疑你想在之后只计算 euro_result = euro * qty; ,你已经收集了数量的值。

问题是你在用户输入任何数据之前将 qty 乘以汇率。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top