Advertisement
GabrielHr00

03. Vacation

Apr 7th, 2024
569
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.17 KB | None | 0 0
  1. package _05_WhileLoops;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Vacation {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.         double tripPrice = Double.parseDouble(scanner.nextLine());
  9.         double currentMoney = Double.parseDouble(scanner.nextLine());
  10.  
  11.         int spendRow = 0;
  12.         int all = 0;
  13.  
  14.         while (true) {
  15.             String name = scanner.nextLine();
  16.             double n = Double.parseDouble(scanner.nextLine());
  17.             all++;
  18.  
  19.             if(name.equals("save")) {
  20.                 currentMoney += n;
  21.                 spendRow = 0;
  22.  
  23.             } else if(name.equals("spend")) {
  24.                 currentMoney -= n;
  25.                 spendRow++;
  26.  
  27.                 if(currentMoney < 0) {
  28.                     currentMoney = 0;
  29.                 }
  30.             }
  31.  
  32.             if(currentMoney >= tripPrice) {
  33.                 System.out.printf("You saved the money for %d days.", all);
  34.                 break;
  35.             }
  36.  
  37.             if(spendRow == 5) {
  38.                 System.out.printf("You can't save the money.%n%d", all);
  39.                 break;
  40.             }
  41.         }
  42.  
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement