myrdok123

P03Vacation

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