【发布时间】:2021-01-14 22:35:48
【问题描述】:
我创建了这个程序来获取用户输入的金额(只能是整数)。它应该打印收到每张账单的数量,但由于某种原因,当我运行代码时,它要求用户输入并且在我输入金额后不打印任何其他内容。我将不胜感激!
#include <stdio.h>
int main() {
double total;
printf("Please enter the amount of money you would like to withdraw: ");
scanf("%lf", &total);
double hundreds;
if (total / 100 >= 1) {
hundreds = total / 100;
} else {
hundreds = 0;
printf("You have received %lf hundred(s)", hundreds);
total -= 100 * hundreds;
}
double fifties;
if (total / 50 >= 1) {
fifties = total / 50;
} else {
fifties = 0;
printf("You have received %lf fifty(s)", fifties);
total -= 50 * fifties;
}
double twenties;
if (total / 20 >= 1) {
twenties = total / 20;
} else {
twenties = 0;
printf("You have received %lf twenty(s)", twenties);
total -= 20 * twenties;
}
double tens;
if (total / 10 >= 1) {
tens = total / 10;
} else {
tens = 0;
printf("You have received %lf ten(s)", tens);
total -= 10 * tens;
}
double fives;
if (total / 5 >= 1) {
fives = total / 5;
} else {
fives = 0;
printf("You have received %lf five(s)", fives);
total -= 5 * fives;
}
double ones;
if (total >= 1) {
ones = total;
} else {
ones = 0;
printf("You have received %lf one(s)", ones);
total -= total;
}
}
【问题讨论】:
-
为什么要使用浮点数?
-
只能是整数 这就是线索。使用
int而不是double,数学[可能] 会更好。 -
请显示您的确切运行日志。当我运行你的代码时,我确实得到了输出(尽管是错误的值)。您可能希望将
"\n"添加到打印语句的末尾,以确保它们立即刷新到输出。你有没有做过任何基本的调试?比如在调试器中运行你的代码? -
给我的问题说将值存储为双精度值,我也尝试使用 int 并且发生了同样的事情。我已经调试了我的代码,它应该可以正常运行。
-
问题是您将
printfs 放入else分支。将它们移到if..else... 块之后。 (还有其他问题,但这会导致很多输入情况下没有打印)
标签: c