int moneyUser; Scanner money = new Scanner(System.in); System.out.print("Привет! Сколько нужно разменять? : "); moneyUser = money.nextInt(); int coin = 1; while (moneyUser > 0){ if(moneyUser > 25){ moneyUser -= 25; coin++; }else if (moneyUser < 25){ moneyUser -= 10; coin++; }else if (moneyUser < 10){ moneyUser -= 5; coin++; } else if (moneyUser < 5){ moneyUser -= 1; coin++; } else { System.out.println(coin); } } 

Given: coins with the nominal value of 25, 10, 5, 1 cent

The program should:

  Спросить пользователя, сколько сдачи нужно выдать Посчитать минимальное количество монет, с помощью которых можно это сделать 
  • What does "not go in order" mean? What exactly is happening and what does not suit you? - D-side
  • D-side, for example, if you enter the number 25, then the number of coins to deposit, in this case, will be based on idea 1, and it turns out but the cycle displays this number infinitely, but if you enter 35, you should get 2 coins, 25 and 10 , but the answer is not even displayed, the program simply ends Shl. I am a novice, I can not even properly degenerate) - Faridun
  • 2
    Initially, 25 coins - subtract 25 (becomes 0), add a coin and go back to the beginning. For zero coins, the first one is suitable - the second block (after all, <0 <25): subtract 10 (from zero!) And return to the beginning. Already something went wrong, is not it? - D-side

1 answer 1

Be careful, the program loops because you didn’t correctly spell out the conditions in if . If you enter the number 25, more than one condition is not met ( 25 > 25 = false and 25 < 25 = false ). Correct the if(moneyUser > 25) condition on if(moneyUser >= 25) and everything will work. You also need to fix the else if (moneyUser < 25) to else if (moneyUser < 25 && moneyUser >= 10) , because if, say, you enter the number 2, the last three cycles can theoretically be executed: else if (moneyUser < 25) , else if (moneyUser < 10) , else if (moneyUser < 5) and you can get a negative number.

  • thanks, I really didn’t notice) I’ll continue to look more closely - Faridun