Advertisement
myrdok123

05. Coins - Using else{break;}

May 28th, 2023 (edited)
1,027
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.87 KB | None | 0 0
  1. package L05_WhileLoop;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class P05_Coins {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.  
  10.         //прочитаме входа -> сума, която трябва да върнем като ресто
  11.  
  12.         double sum = Double.parseDouble(scanner.nextLine());
  13.  
  14.         //превръщаме сумата в стотинки
  15.         double totalSumCoins = sum * 100;
  16.  
  17.         // създаваме променлива, в която ще броим колко монети сме върнали
  18.         int countCoins = 0;
  19.  
  20.         while (totalSumCoins > 0){
  21.  
  22.             //проверяваме колко е стойността на totalCoins -> ако totalCoins > 200 -> totalCoins - 200;  ако totalCoins > 100 -> totalCoins - 100
  23.  
  24.             if(totalSumCoins >= 200){
  25.                 countCoins++;
  26.                 totalSumCoins -= 200;
  27.             } else if (totalSumCoins >= 100) {
  28.                 countCoins++;
  29.                 totalSumCoins -= 100;
  30.  
  31.             }else if (totalSumCoins >= 50) {
  32.                 countCoins++;
  33.                 totalSumCoins -= 50;
  34.  
  35.             }else if (totalSumCoins >= 20) {
  36.                 countCoins++;
  37.                 totalSumCoins -= 20;
  38.  
  39.             }else if (totalSumCoins >= 10) {
  40.                 countCoins++;
  41.                 totalSumCoins -= 10;
  42.  
  43.             }else if (totalSumCoins >= 5) {
  44.                 countCoins++;
  45.                 totalSumCoins -= 5;
  46.  
  47.             }else if (totalSumCoins >= 2) {
  48.                 countCoins++;
  49.                 totalSumCoins -= 2;
  50.  
  51.             }else if (totalSumCoins >= 1) {
  52.                 countCoins++;
  53.                 totalSumCoins -= 1;
  54.  
  55.             }else {
  56.                 break;
  57.             }
  58.  
  59.  
  60.         }
  61.  
  62.         System.out.println(countCoins);
  63.  
  64.  
  65.  
  66.     }
  67. }
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement