我正在尝试根据百分比计算 Markdown 。如果我手写出来,它会像下面的方程式一样出现,一个简单的 x = x - (10% of x) 或 new_price = old_price - (10% of old_price)。所以 100 会变成 90,90 会变成 81,等等。我认为。我不确定我是不是在胡思乱想,但是当我运行它时,它只是永远循环“90”作为输出。
#include <stdio.h>
#include <math.h>
int pricedecrease(int x)
{
x = x - (x / 10.0);
return x;
}
int main(void)
{
int price = 100;
while(price > 3)
{
printf("%d\n", pricedecrease(price));
}
}
最佳答案
您需要在循环中更新价格变量。调用 pricedecrease
函数不会修改 price
变量。
#include <stdio.h>
#include <math.h>
int pricedecrease(int x)
{
x = x - (x / 10.0);
return x;
}
int main(void)
{
int price = 100;
while(price > 3)
{
printf("%d\n", price);
price = pricedecrease(price); // <- need to update price variable
}
}
关于c - 为什么这个等式不像我期望的那样循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25209277/