#include <cs50.h>
#include <stdio.h>
int main(void) {
printf("Enter your change: ");
int pennies = 0, nickels = 0, dimes = 0, quarters = 0;
float change = GetFloat();
while (change > 0) {
if (change >= 0.25) {
quarters++;
change -= 0.25;
}
else if (change >= 0.10) {
dimes++;
change -= 0.10;
}
else if (change >= 0.05) {
nickels++;
change -= 0.05;
}
else if (change >=0.01) {
pennies++;
change -= 0.01;
}
// force break
else {
printf("%1.2f - Num. of change left\n", change);
break;
}
}
printf("Quarters: %d\n", quarters);
printf("Dimes: %d\n", dimes);
printf("Nickels: %d\n", nickels);
printf("Pennies: %d\n", pennies);
return 0;
}
您好,我目前是 C 的新手,我正在在线上哈佛的 CS50 课程。“更改”变量似乎下降到 0.00 而没有停止 while 循环。这迫使我在最后输入“break”。我的代码有什么问题?
顺便说一下,这是来自问题集 1。