C
C
coachpotato2020-03-30 16:29:50
C++ / C#
coachpotato, 2020-03-30 16:29:50

Implement a condition in a loop or a loop in a loop?

When entering data in textbox2(Cost) and textbox1(Term) and pressing the button, the DataGridView2 table is formed.
First, the amount is calculated as % of the norm of the balance
. It is necessary that when the value in the column Remaining becomes less than/equal to 20% of the cost (the value in textbox2), then the value in the column amount is calculated differently: balance/number of months remaining.
I implemented this as a condition in the loop, but then the amount remains unchanged, but should change every month
5e81f405eb0cd002521223.jpeg

private void button3_Click(object sender, EventArgs e)
        {
            int nextIndex = 1;
            int time = Convert.ToInt32(textBox1.Text); 
            double norma = Math.Round((double)2 / Convert.ToInt32(textBox1.Text) * 100, 2); //норма
            double ost= Math.Round((double)Convert.ToInt32(textBox2.Text),2); // остаток
            double summa = Math.Round((double)ost / 100 * norma, 2); // сумма в месяц
            double proc = Math.Round((double)Convert.ToInt32(textBox2.Text) / 100 * 20, 2); // 20% от стоимости
            for (int i = 0; i < Convert.ToInt32(textBox1.Text); i++)
            {
               
                dataGridView2.Rows.Add();
                dataGridView2.Rows[i].Cells[0].Value = nextIndex.ToString();
                dataGridView2.Rows[i].Cells[1].Value = Math.Round(ost,2).ToString();
                dataGridView2.Rows[i].Cells[2].Value = Math.Round(summa,2).ToString();
                dataGridView2.Rows[i].Cells[3].Value = Math.Round(norma,2).ToString() + "%";
                nextIndex++;
                ost -= summa;

                if (ost <= proc)
                {
                    summa = Math.Round((double)ost / (time - (nextIndex - 1)), 2);
                }
                else { summa = Math.Round((double)ost / 100 * norma, 2); }
            }
            
        }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question