由另一个变量乘以变量?

Zac*_*ith 2 c calculator

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

我把它设置成这样,

    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);
Run Code Online (Sandbox Code Playgroud)

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

mjv*_*mjv 7

这个错误就是这条线

euro_result = euro * qty;
Run Code Online (Sandbox Code Playgroud)

需要在数量后读入


dtb*_*dtb 7

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);
Run Code Online (Sandbox Code Playgroud)

  • 这是一个普通的初学者的反应,认为他们是愚蠢的:-) (2认同)