【问题标题】:Giving lowest amount of bills for a user inputted amount in C为用户在 C 中输入的金额提供最低金额的账单
【发布时间】: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


【解决方案1】:

程序有误。只有小于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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-17
    • 2013-10-20
    • 1970-01-01
    • 2014-12-13
    • 2019-07-29
    • 2013-06-29
    • 1970-01-01
    相关资源
    最近更新 更多