Advertisement
myrdok123

04. Clever Lily

May 21st, 2023
828
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.71 KB | None | 0 0
  1. package L04_ForLoop;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class P04_CleverLily {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.  
  10.  
  11.         int age = Integer.parseInt(scanner.nextLine());
  12.         double priceWashingMachine = Double.parseDouble(scanner.nextLine());
  13.         int priceToy = Integer.parseInt(scanner.nextLine());
  14.  
  15.         double sum = 0;
  16.         int countToys = 0;
  17.  
  18.         // for -> минавеме през всяка от годините(рождените дни) -> за четните получава пари, а за нечетните получава играчка
  19.  
  20.         //int moneyEvenAge = 10;
  21.  
  22.         for (int currentAge = 1; currentAge <= age ; currentAge++) {
  23.  
  24.             //проверка дали годината е четна или нечетна
  25.             if (currentAge % 2 == 0){
  26.                 /*sum += moneyEvenAge;
  27.                 moneyEvenAge += 10;
  28.                 sum--;*/
  29.  
  30.                 //парите, които получава
  31.                 sum = sum + (currentAge * 5 - 1);// sum += currentAge * 5 - 1
  32.  
  33.             }else {
  34.                 //нечетна година/рожден ден -> увеличаваме броя на играчките
  35.                 countToys++;
  36.             }
  37.  
  38.  
  39.         }
  40.  
  41.  
  42.         //към досегашната сума прибавяме и парите от играчките
  43.         sum += countToys * priceToy;
  44.  
  45.         double diff = Math.abs(priceWashingMachine - sum);
  46.  
  47.         if (sum >= priceWashingMachine){
  48.  
  49.             System.out.printf("Yes! %.2f", diff);
  50.         }else {
  51.             System.out.printf("No! %.2f", diff);
  52.         }
  53.  
  54.  
  55.     }
  56. }
  57.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement