c - 为用户在 C 中输入的金额提供最低金额的账单
问题描述
我创建了这个程序来获取用户输入的金额(只能是整数)。它应该打印收到每张账单的数量,但由于某种原因,当我运行代码时,它要求用户输入并且在我输入金额后不打印任何其他内容。我会很感激一些帮助!
#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;
}
}
解决方案
程序有误。仅在小于 时打印票据数量1
:
#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;
}
}
double
此外,在除以钞票面值时不应使用算术。整数运算将正确计算票据的数量。
这是修改后的版本:
#include <stdio.h>
int main() {
int total;
printf("Please enter the amount of money you would like to withdraw: ");
if (scanf("%d", &total) != 1)
return 1;
if (total <= 0) {
printf("You have received no bills\n");
return 0;
}
int hundreds = total / 100;
if (hundreds) {
printf("You have received %d hundred(s)\n", hundreds);
total -= 100 * hundreds;
}
int fifties = total / 50;
if (fifties) {
printf("You have received %d fifty(s)\n", fifties);
total -= 50 * fifties;
}
int twenties = total / 20;
if (twenties) {
printf("You have received %d twenty(s)\n", twenties);
total -= 20 * twenties;
}
int tens = total / 10;
if (tens) {
printf("You have received %d ten(s)\n", tens);
total -= 10 * tens;
}
int fives = total / 5;
if (fives) {
printf("You have received %d five(s)\n", fives);
total -= 5 * fives;
}
int ones = total;
if (ones) {
printf("You have received %d one(s)\n", ones);
total -= total;
}
return 0;
}
推荐阅读
- json - 使用 pyspark 解析 JSON 时嵌套动态模式不起作用
- python - 如何将键与列python对齐?
- reactjs - 我不知道为什么我的应用不显示广告?
- android - 恢复db文件后重新打开房间数据库
- python - os.walk() 运行代码时不经过子目录
- python - 有什么方法可以在 pandas 中构建有效的特征矩阵以进行二元分类?
- php - 如何检索将值存储在php中嵌套数组中的json响应对象
- javascript - 如何在使用 socket.io 发出日期后格式化日期
- typescript - webpack 中的 vue/ts 应用程序显示:无法挂载组件:未定义模板或渲染函数
- ruby - Ruby - 如何在使用 Nokogiri 的 div 之后检索文本