silviasj

Vacation

Apr 4th, 2020
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.50 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Vacation_03 {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.         double needMoney = Double.parseDouble(scanner.nextLine());
  7.         double money = Double.parseDouble(scanner.nextLine());
  8.  
  9.         //СТОП
  10.         //1. ако 5 последователни дни харчи -> break
  11.         //2. ако събере парите за екскурзията -> money >= needMoney
  12.         //продължава да събира -> money < needMoney
  13.         int countAllDays = 0;
  14.         int countSpendDays = 0;
  15.         while (money < needMoney) {
  16.                 //събира пари
  17.             String action = scanner.nextLine(); //save или spend
  18.             double price = Double.parseDouble(scanner.nextLine());
  19.             countAllDays++;
  20.             if (action.equals("save")) {
  21.                 money += price;
  22.                 countSpendDays = 0;
  23.             } else if (action.equals("spend")) {
  24.                 countSpendDays++;
  25.                 money -= price;
  26.                 if (money < 0) {
  27.                     money = 0;
  28.                 }
  29.             }
  30.  
  31.             if(countSpendDays == 5){
  32.                 System.out.println("You can't save the money.");
  33.                 System.out.print(countAllDays);
  34.                 break;
  35.             }
  36.  
  37.         }
  38.         if (money >= needMoney) {
  39.             System.out.printf("You saved the money for %d days.", countAllDays);
  40.         }
  41.     }
  42. }
Add Comment
Please, Sign In to add comment