Advertisement
ivanmitkoff

03. Vacation (While Loop - Exercise)

Jun 15th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.37 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.  
  7.         double tripPrice = Double.parseDouble(scanner.nextLine());
  8.         double savedMoney = Double.parseDouble(scanner.nextLine());
  9.  
  10.         String action = scanner.nextLine();
  11.         double sum = Double.parseDouble(scanner.nextLine());
  12.         int daysCounter = 0;
  13.         int spendCounter = 0;
  14.  
  15.         while (savedMoney < tripPrice) {
  16.             daysCounter++;
  17.             if (action.equals("spend")) {
  18.                 savedMoney -= sum;
  19.                 spendCounter++;
  20.                 if (savedMoney < 0) {
  21.                     savedMoney = 0;
  22.                 }
  23.                 if (spendCounter == 5) {
  24.                     System.out.printf("You can't save the money.%n%d", spendCounter);
  25.                     break;
  26.                 }
  27.             } else if (action.equals("save")) {
  28.                 savedMoney += sum;
  29.                 if (savedMoney < 0) {
  30.                     savedMoney = 0;
  31.                 }
  32.             }
  33.             if (savedMoney >= tripPrice) {
  34.                 System.out.printf("You saved the money for %d days.", daysCounter);
  35.                 break;
  36.             }
  37.             action = scanner.nextLine();
  38.             sum = Double.parseDouble(scanner.nextLine());
  39.         }
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement