-2
我的代碼怎麼不打印int
叫coins
? 運行調試系統時看不到任何錯誤。我的代碼不打印稱爲硬幣的變量
#include <cs50.h>
#include <stdio.h>
int main(void)
{ // getting the user validation
printf("please enter a amount of change \n");
int change = get_int();
int coins = 0;
if (change > 0 && change <= 0 && change != 0)
printf("Change: %i\n", change);
if (change > 0)
{
printf("Change: %i\n", change);
}
if (change < 0)
{
printf("please enter a positve number \n");
}
if (change == 0)
{
printf("Coins: 0 \n");
}
//were it counts the amount of coins
float changef = (int)change;
float qaurter = 0.25;
float dime = 0.10;
float nickel = 0.05;
float penny = 0.01;
while (changef/qaurter)
{
coins++;
return changef - qaurter;
}
while (changef/dime)
{
coins++;
return changef - dime;
}
while (changef/nickel)
{
coins++;
return changef - nickel;
}
while (changef/penny)
{
coins++;
return changef - penny;
}
printf("Coins: %i\n",coins);
}
在'if(change> 0 && change <= 0 && change!= 0)前兩個條件不能*都是真實的。 –
請學會格式化您的代碼,並將其格式化。閱讀未格式化的代碼很煩人。使用模塊運算符'%'。除此之外,你是什麼意思「不打印?」在處理貨幣時,你也不應該使用'float'和'double',這會導致長期的錯誤結果 – Pablo
請閱讀[浮點運算是否被破壞?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken)這裏有很多許多「改變」的問題。使用浮點類型很少令人滿意。 –