我认为问题在嵌套之前开始,因为在代码结束时它应该输出sales = price * sold
的结果,但它也输出0
#include<stdio.h>
main() {
int num, price, sold, sales;
float compercent, commision;
printf("Enter Unit Price: ");
scanf("%d", &price);
printf("Enter Quantity Sold: ");
scanf("%d", &sold);
sales = price * sold;
if (sales >= 50000) {
compercent = 0.1;
}
else if (sales >= 40000 && sales < 50000) {
compercent = 0.08;
}
else if (sales >= 30000 && sales < 40000) {
compercent = 0.06;
}
else if (sales >= 20000 && sales < 30000) {
compercent = 0.04;
}
else if (sales >= 10000 && sales < 20000) {
compercent = 0.02;
}
else if (sales >= 5000 && sales < 10000) {
compercent = 0.01;
}
else if(sales<5000){
compercent = 0;
}
commision = sales * compercent;
printf("Sales: ");
printf("sales = %f", &sales);
printf("Commision: ");
printf("%f", &commision);
}
使用prinf
时,不需要提供变量作为引用。该函数需要整数或浮点数,而不是指针。
这应该可以解决它:
printf("sales = %in", sales);
printf("Commision: ");
printf("%f", commision);
不要在 printf 语句中使用 '&' 打印变量值。它通常会打印一些垃圾值(或 0,如您的情况(。 我认为这应该可以解决您的问题:
printf("Sales: ");
printf("sales = %f", sales);
printf("Commision: ");
printf("%f", commision);