我创建了这个程序来接受用户输入的金额(只能是整数)。它应该打印收到的每张账单的数量,但出于某种原因,当我运行代码时,它要求用户输入并且在我输入金额后不打印任何其他内容。我将不胜感激!
#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;
}
}
答案 0 :(得分:0)
程序缩进错误。仅当少于 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;
}