Dianov

While Loop - Exercise (03. Vacation)

Dec 30th, 2020
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.51 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Vacation
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             double neededMoney = double.Parse(Console.ReadLine());
  14.             double money = double.Parse(Console.ReadLine());
  15.             int days = 0;
  16.             int daysSpending = 0;
  17.  
  18.             while (true)
  19.             {
  20.                 string action = Console.ReadLine();
  21.                 double amount = double.Parse(Console.ReadLine());
  22.                 days++;
  23.                 if (action == "spend")
  24.                 {
  25.                     money = money - amount;
  26.                     if (money <= 0)
  27.                     {
  28.                         money = 0;
  29.                     }
  30.                     daysSpending += 1;
  31.                     if (daysSpending >= 5)
  32.                     {
  33.                         Console.WriteLine("You can't save the money.");
  34.                         Console.WriteLine(days);
  35.                         break;
  36.                     }
  37.                 }
  38.                 else if (action == "save")
  39.                 {
  40.                     daysSpending = 0;
  41.                     money = money + amount;
  42.                     if (money >= neededMoney)
  43.                     {
  44.                         Console.WriteLine($"You saved the money for {days}" + " days.");
  45.                         break;
  46.                     }
  47.                 }
  48.             }
  49.         }
  50.     }  
  51. }
Advertisement
Add Comment
Please, Sign In to add comment