本文介绍了为什么此代码忽略数组中的0.10和0.01值。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 这是我的自助结账工作的核心,我想知道为什么它不能正常工作。它忽略了0.10和0.01的值,我不知道为什么。请帮我。 int main() { float howMuch; 浮动付款; 浮动货币[13] = {100,50,20,10,5,2,1,0.50,0.20,0.10,0.05,0.02,0.01}; scanf(%f,& howMuch); while(howMuch> 0) { scanf(%f,& payment); howMuch - =付款; } howMuch = -1 * howMuch; for(int i = 0; i< 13; i ++) { while(money [i]< = howMuch) { howMuch - =钱[I]; printf(%。2f,money [i]); } } printf(\ n); 返回0; } 我的尝试: 没什么,我认为它应该可行。解决方案 因为你使用浮点数! 浮点数不是确切的值。 如果你说你以美分工作,你可以切换到整数。 某些语言有货币 datatyp e with是缩放整数。 This is the core from my self-checkout assignment and I am wondering why it is not working properly. It is ignoring 0.10 and 0.01 value, and I do not know why. Please help me. int main(){float howMuch;float payment;float money[13] = {100, 50, 20, 10, 5, 2, 1, 0.50, 0.20, 0.10, 0.05, 0.02, 0.01};scanf("%f", &howMuch);while(howMuch > 0 ){ scanf("%f", &payment); howMuch -= payment;}howMuch = -1*howMuch;for(int i = 0; i < 13; i++){ while(money[i] <= howMuch) { howMuch -= money[i]; printf("%.2f ", money[i]); }}printf("\n");return 0;}What I have tried:Nothing, I just think it should work. 解决方案 Because you are using floats!Floats are not exact values.If you say that you work in cents, you can switch to integers.Some languages have currency datatype with is scaled integer. 这篇关于为什么此代码忽略数组中的0.10和0.01值。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-27 19:11