Advertisement
silviasj

vacation while loop

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