Advertisement
myrdok123

03. Vacation

Feb 4th, 2024
719
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.29 KB | None | 0 0
  1. package W05WhileLoop.Exercises;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class P03Vacation {
  6.  
  7.     public static void main(String[] args) {
  8.  
  9.         Scanner scanner = new Scanner(System.in);
  10.  
  11.  
  12.         double neededMoney = Double.parseDouble(scanner.nextLine());
  13.         double availableMoney = Double.parseDouble(scanner.nextLine());
  14.  
  15.  
  16.         int countSpend = 0;
  17.         int countDays = 0;
  18.  
  19.         while (availableMoney < neededMoney){
  20.  
  21.  
  22.             if (countSpend == 5){
  23.                 break;
  24.             }
  25.  
  26.             String command = scanner.nextLine();
  27.             double currentSum = Double.parseDouble(scanner.nextLine());
  28.             countDays++;
  29.  
  30.             if(command.equals("spend")){
  31.                 availableMoney -= currentSum;
  32.                 countSpend++;
  33.             } else if (command.equals("save")) {
  34.                 availableMoney += currentSum;
  35.                 countSpend = 0;
  36.  
  37.             }
  38.  
  39.             if (availableMoney < 0){
  40.                 availableMoney = 0;
  41.             }
  42.  
  43.  
  44.         }
  45.  
  46.  
  47.         if(countSpend == 5){
  48.             System.out.println("You can't save the money.");
  49.             System.out.printf("%d", countDays);
  50.         }else {
  51.             System.out.printf("You saved the money for %d days.", countDays);
  52.         }
  53.     }
  54. }
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement