Advertisement
Spocoman

03. Vacation

Aug 29th, 2024
433
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.08 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Vacation {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.         double neededMoney = Double.parseDouble(scanner.nextLine());
  7.         double budget = Double.parseDouble(scanner.nextLine());
  8.         int spendDays = 0, saveDays = 0;
  9.  
  10.         while (budget < neededMoney && spendDays < 5) {
  11.             String input = scanner.nextLine();
  12.             double money = Double.parseDouble(scanner.nextLine());
  13.             if (input.equals("spend")) {
  14.                 spendDays++;
  15.                 if (money >= budget) {
  16.                     budget = 0;
  17.                 } else {
  18.                     budget -= money;
  19.                 }
  20.             } else {
  21.                 budget += money;
  22.                 spendDays = 0;
  23.             }
  24.             saveDays++;
  25.         }
  26.  
  27.         if (budget < neededMoney) {
  28.             System.out.printf("You can't save the money.\n%d\n", saveDays);
  29.         } else {
  30.             System.out.printf("You saved the money for %d days.", saveDays);
  31.         }
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement