首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >嵌套多个while循环- CS50 greedy.c减少

嵌套多个while循环- CS50 greedy.c减少
EN

Stack Overflow用户
提问于 2017-11-06 23:33:39
回答 1查看 134关注 0票数 0

我对编程非常陌生,在理解如何将while循环与其他循环结合使用方面存在一些困难。

您可以从我的代码中看到,我可以让硬币计数增加,但我认为应该使用while循环而不是do-while循环来逐行计算更改总数。

也许我让它变得比所需的更复杂,但是多个while循环是否是计算更改值的最佳方法呢?

任何提示或提示都非常感谢!

代码语言:javascript
复制
int main(void)
{
printf("O hai! ");

float change;

do
{
    printf("How much change is owed?\n");
    change = get_float();
}
while (change <= 0);

int n = (change * 100);     // multiply change by 100 to get int in cents(n)

int coin = 0;               // new int to represent amount of coins


coin++;
int quart = n - 25;     // subtract 25c from n

coin++;
int dime = quart - 10;  // subtract 10c from quart

coin++;
int nick = dime - 5;    // and so on

coin++;
int penny = nick - 1;   // and so on

penny = 0;              // and this is only here because I had an unused int (penny)


printf("%i\n", coin);
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-11-07 00:19:01

时间循环工作得很好。但是,您将希望使其大于0,而不是像目前的那样小于或等于,而且,您也不需要定义季度/一角和所有这些,您可以使用if语句,比如if (剩下的美分比硬币的价值更多),而不是硬币的-= (这是每次碰到硬币时都会减少的方法)。试着做这些改变,我可以帮你更多的忙

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47147617

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档