在线编译器 C

#include <stdio.h> int main(void) { int price,quantity,discount; scanf("%d %d %D",&price,&quantity,&discount); double total=price*quantity*(100-discount)/100; printf("C=%.2f\2n",total); return 0; }
这段代码的目的是读取商品单价、数量和折扣百分比,然后计算并输出折扣后的总金额。代码中存在几个问题。

- 在 `scanf` 中,第三个格式说明符写成了 `%D`(大写D),而C语言中格式说明符是区分大小写的,应该使用小写 `%d` 来读取整数。
- 计算 `total` 时,`(100-discount)/100` 是整数除法,结果会截断小数部分,导致计算结果错误。你需要确保除法得到浮点数结果。
- `printf` 中的 `\2n` 看起来像是一个拼写错误,应该是 `\n` 换行符。