svephoto

Vacation [C#]

Feb 12th, 2022 (edited)
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.17 KB | None | 0 0
  1. using System;
  2. //using System.Collections.Generic;
  3. //using System.Linq;
  4.  
  5. namespace _3._Vacation
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             double neededMoney = double.Parse(Console.ReadLine());
  12.             double ownedMoney = double.Parse(Console.ReadLine());
  13.             int brspend = 0;
  14.             int brall = 0;
  15.             //double sumSpendings = 0; Нямаме нужда от тази и от следващите 5 променливи.
  16.             //double sumSavings = 0;
  17.             //List<double> spendings = new List<double>();
  18.             //List<double> savings = new List<double>();
  19.             //double moneyAfterSpendings = 0;
  20.             //double moneyAfterSavings = 0;
  21.  
  22.             while (ownedMoney < neededMoney && brspend < 5) // Нужен ни е while цикъл с тези 2 условия, а при теб е for цикъл, който ти въртиш до int.MaxValue, което не е правилно.
  23.             {
  24.                 brall++; // Можем да увеличаваме общия брой дни само тук, вместо на 2 места в двете проверки.
  25.  
  26.                 string action = Console.ReadLine();
  27.                 double sum = double.Parse(Console.ReadLine());
  28.  
  29.                 if (action.Equals("spend"))
  30.                 {
  31.                     //spendings.Add(sum);
  32.                     //sumSpendings = spendings.Sum();
  33.                     //brall++;
  34.                     //moneyAfterSpendings = ownedMoney - sumSpendings;
  35.                     brspend++;
  36.                     ownedMoney -= sum; // От спестените пари изваждаме въведената от конзолата сума.
  37.  
  38.                     //if (moneyAfterSpendings <= 0)
  39.                     if (ownedMoney <= 0) // Проверяваме с ownedMoney, нямаме нужда от допълнителни променливи.
  40.                     {
  41.                         ownedMoney = 0;
  42.                     }
  43.                 }
  44.                 else if (action.Equals("save"))
  45.                 {
  46.                     //savings.Add(sum);
  47.                     //sumSavings = savings.Sum();
  48.                     //brall++;
  49.                     //moneyAfterSavings = sumSavings + moneyAfterSpendings;
  50.                     brspend = 0;
  51.                     ownedMoney += sum; // Към спестените пари прибавяме въведената от конзолата сума.
  52.  
  53.                     //if (moneyAfterSavings >= neededMoney)
  54.                     if (ownedMoney >= neededMoney) // Отново проверяваме с ownedMoney, като и тук нямаме нужда от допълнителни променливи.
  55.                     {
  56.                         Console.WriteLine("You saved the money for {0} days.", brall);
  57.                         break;
  58.                     }
  59.                 }
  60.  
  61.                 if (brspend == 5)
  62.                 {
  63.                     Console.WriteLine("You can't save the money.");
  64.                     Console.WriteLine(brall);
  65.                     break;
  66.                 }
  67.             }
  68.         }
  69.     }
  70. }
  71.  
Add Comment
Please, Sign In to add comment