Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package S5_WhileLoops;
- import java.util.Scanner;
- public class Vacation {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- double tripPrice = Double.parseDouble(scanner.nextLine());
- double currentMoney = Double.parseDouble(scanner.nextLine());
- // save days info
- int spendInRowCount = 0;
- int allDaysCount = 0;
- // read first command
- String command = scanner.nextLine();
- while(true) {
- // read spend or save
- String operator = command;
- // read money for spending/saving
- double price = Double.parseDouble(scanner.nextLine());
- allDaysCount++;
- // check if we should spend or save
- if(operator.equals("spend")) {
- spendInRowCount++;
- currentMoney = currentMoney - price;
- // set current money to 0 when we spend more than we have
- if(currentMoney < 0) {
- currentMoney = 0;
- }
- } else if(operator.equals("save")){
- currentMoney = currentMoney + price;
- spendInRowCount = 0;
- }
- // if we saved enough money - break
- if(currentMoney >= tripPrice) {
- break;
- }
- // if we spent too much days spending money - break
- if(spendInRowCount == 5){
- break;
- }
- command = scanner.nextLine();
- }
- // check if we have enough money for trip
- if (currentMoney >= tripPrice) {
- System.out.printf("You saved the money for %d days.", allDaysCount);
- } else {
- System.out.printf("You can't save the money.%n%d", allDaysCount);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement