I did a task from one famous course. The task is that we have a debt and we can pay it immediately or in parts. But the condition is that your next payment should be more than the previous one. I coped with the task itself, but looking at the code, I think that it is very complicated and complicated.
Here he is:

using System; class Program { static void Main() { int maxSum = 700; int payment = 0; int previousPayment = 0; int moneyToPay = 0; while(maxSum != 0) { do { Console.WriteLine("Введите сумму платежа"); payment = int.Parse(Console.ReadLine()); //previousPayment = payment; moneyToPay = debt(payment, ref maxSum, ref previousPayment); if (moneyToPay == 0) { break; } } while (false); if(moneyToPay == -1) { break; } } Console.ReadKey(); } static int debt(int a,ref int maxSum, ref int previousPayment) { if (a == maxSum) { Console.WriteLine("Долг погашен"); return maxSum = 0; } else if (a < maxSum) { int Debt = maxSum - a; if(a < previousPayment) { Console.WriteLine("Вы должны оплатить сумму больше чём {0}", previousPayment); return -1; } Console.WriteLine("Ваш долг составляет:{0}", Debt); maxSum = Debt; previousPayment = a; return Debt; } else { Console.WriteLine("Долг погашен"); Console.WriteLine("Переполнение составляет:{0}", a - maxSum); return maxSum = 0; } } } 
  • one
    What is the point of doing do{...}while(false) ? - Grundy
  • @tCode to further reset the maxSum variable which is passed by ref - Grundy
  • @Grundy yes, thanks, already understood, did not see ref maxSum - tCode

1 answer 1

Your code is really unnecessarily confused, most of the errors are typical for beginners, the main thing is that you yourself have found the solution to the problem, this is a huge plus.

A bit of error.

  • The do{...}while(false) construct runs strictly once, thus useless and can be removed.
  • while(maxSum != 0) careful with the use of such a condition, since the cycle will be completed strictly with maxSum == 0 , in this problem it was better to use the while(maxSum > 0) condition while(maxSum > 0) .
  • static int debt(int a,ref int maxSum, ref int previousPayment) very good that you could figure out how to pass parameters by reference, but in this case it would be more correct to declare maxSum and previousPayment private static fields of the class, and contact them directly from method. This is similar to global variables, but only within a class.
  • the sequence of actions is a little confused and you can see, so to speak, the course of your thoughts in the search process. It was necessary to look once again at the solution found and try to find repetitive moments. But this is normal to begin with and will come with experience if you do not stop there.

The code can be considerably reduced without affecting the “readability”, for example like this:

 void Main() { int debt = 700; int lastPayment = 0; while(debt > 0) { Console.WriteLine("Введите сумму платежа"); int payment = int.Parse(Console.ReadLine()); while(payment <= lastPayment) { Console.WriteLine($"Вы должны оплатить сумму больше чём {lastPayment}"); payment = int.Parse(Console.ReadLine()); } lastPayment = payment; debt -= payment;//debt = debt - payment на всякий случай } Console.WriteLine("Долг погашен"); //как только долг стал меньше или равен 0, его модуль равен сумме переплаты, этим и воспользуемся. if(debt < 0) Console.WriteLine($"Переплата составляет:{-debt}"); } 
  • worth at least list the errors - Grundy
  • @Grundy seems to have listed everything that can be suggested briefly without a lecture - rdorn