gpt4 book ai didi

c - "new_dollars = dollars + cents/100"没有变化

转载 作者:行者123 更新时间:2023-11-30 18:29:32 25 4
gpt4 key购买 nike

在输出中,new_dollars总是显示美元数量,所以我不知道如何解决这个问题。请帮我找出问题所在。

int main(void)
{
int dollars, cents, count, new_dollars;

for ( count = 1; count <= 10; ++count){
printf ("Enter dollars: ");
scanf ("%i", &dollars);

printf ("Enter cents: ");
scanf ("%i", &cents);

if ( cents >= 100 ){
cents = cents % 100;
new_dollars = dollars + cents / 100;
printf ("%i\n", new_dollars);
printf ("$%i.%2i\n\n", new_dollars, cents);
}
else {
printf ("$%i.%2i\n\n", dollars, cents);
}
}

return 0;
}

最佳答案

评论者关于整数除法的看法是正确的。试试这个,它交换了执行计算的两行顺序,因此当您需要时,分仍然具有有用的值:

int main(void)
{
int dollars, cents, count, new_dollars;

for ( count = 1; count <= 10; ++count){
printf ("Enter dollars: ");
scanf ("%i", &dollars);

printf ("Enter cents: ");
scanf ("%i", &cents);

if ( cents >= 100 ){
new_dollars = dollars + cents / 100;
cents = cents % 100;
printf ("%i\n", new_dollars);
printf ("$%i.%2i\n\n", new_dollars, cents);
}
else {
printf ("$%i.%2i\n\n", dollars, cents);
}
}

return 0;
}

关于c - "new_dollars = dollars + cents/100"没有变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37558443/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com