Advertisement
CoineTre

03.Vacation

Nov 12th, 2020
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.21 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 ownedMoney = Double.parseDouble(scanner.nextLine());
  8.         int days = 0;
  9.         int countSpend = 0;
  10.         while (ownedMoney < neededMoney) {
  11.             String moneyType = scanner.nextLine();
  12.             double moneyTotal = Double.parseDouble(scanner.nextLine());
  13.             days++;
  14.             if ("spend".equals(moneyType)) {
  15.                 countSpend++;
  16.                 ownedMoney -= moneyTotal;
  17.                 if (ownedMoney < 0) {
  18.                     ownedMoney = 0;
  19.                 }
  20.                 if (countSpend == 5) {
  21.                     System.out.println("You can't save the money.");
  22.                     System.out.println(days);
  23.                     break;
  24.                 }
  25.             } else if ("save".equals(moneyType)) {
  26.                 countSpend=0;
  27.                 ownedMoney += moneyTotal;
  28.             }
  29.         }
  30.         if (ownedMoney>=neededMoney){
  31.             System.out.printf("You saved the money for %d days.", days);
  32.  
  33.         }
  34.     }
  35. }
  36.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement