Advertisement
Spocoman

Family Trip

Sep 5th, 2024
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.61 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.         double budget = Double.parseDouble(scanner.nextLine());
  7.         int overnights = Integer.parseInt(scanner.nextLine());
  8.         double overnightPrice = Double.parseDouble(scanner.nextLine());
  9.         int additionalCosts = Integer.parseInt(scanner.nextLine());
  10.  
  11.         if (overnights > 7) {
  12.             overnightPrice *= 0.95;
  13.         }
  14.  
  15.         budget -= overnightPrice * overnights + 0.01 * budget * additionalCosts;
  16.  
  17.         if (budget >= 0) {
  18.             System.out.printf("Ivanovi will be left with %.2f leva after vacation.\n", budget);
  19.         } else {
  20.             System.out.printf("%.2f leva needed.\n", Math.abs(budget));
  21.         }
  22.     }
  23. }
  24.  
  25. Решение с тернарен оператор:
  26.  
  27. import java.util.Scanner;
  28.  
  29. public class Main {
  30.     public static void main(String[] args) {
  31.         Scanner scanner = new Scanner(System.in);
  32.         double budget = Double.parseDouble(scanner.nextLine());
  33.         int overnights = Integer.parseInt(scanner.nextLine());
  34.         double overnightPrice = Double.parseDouble(scanner.nextLine());
  35.         int additionalCosts = Integer.parseInt(scanner.nextLine());
  36.  
  37.         budget -= overnightPrice * (overnights > 7 ? 0.95 : 1) * overnights + 0.01 * budget * additionalCosts;
  38.  
  39.         System.out.printf(
  40.                 (budget >= 0
  41.                         ? ("Ivanovi will be left with %.2f leva after vacation.\n")
  42.                         : ("%.2f leva needed.\n")), Math.abs(budget)
  43.         );
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement