galinyotsev123

ProgBasics05while-Loop-Y04vacation

Jan 7th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Y04vacation {
  4. public static void main(String[] args) {
  5. Scanner scanner = new Scanner(System.in);
  6.  
  7. double needMoneyForVocation = Double.parseDouble(scanner.nextLine());
  8. double availableMoney = Double.parseDouble(scanner.nextLine());
  9. int countAllDays = 0;
  10. double saveMoney = availableMoney;
  11. int countSpend = 0;
  12.  
  13.  
  14. while (!(saveMoney >= needMoneyForVocation)){
  15. countAllDays++;
  16. String whatToDo = scanner.nextLine();
  17. double money = Double.parseDouble(scanner.nextLine());
  18. switch(whatToDo){
  19. case "spend":
  20. countSpend++;
  21. if(countSpend == 5){
  22. System.out.println("You can't save the money.");
  23. System.out.println(countAllDays);
  24. return;
  25. }
  26. if(money < saveMoney){
  27. saveMoney -= money;
  28. }else {
  29. saveMoney = 0;
  30. }
  31. break;
  32. case "save":
  33. countSpend = 0;
  34. saveMoney += money;
  35. break;
  36. }
  37. }
  38. System.out.printf("You saved the money for %d days.", countAllDays);
  39. }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment