do-while循环冻结程序

时间:2015-06-16 00:37:49

标签: c# loops calculator do-while

我尝试使用do-while循环但在输入输入并启动按钮单击事件后它没有做任何事情。它应该计算出所有年份的数量和列表,直到它<= 40,000。我可以让程序在没有循环但没有循环的情况下运行。

    private double InterestEarned(double AMT, double AIR = 0.07)
    {                        
        return AMT * AIR;
    }

    private double InheritanceAmount(double BAL, double IR, double AIR = 0.07)
    {            
        return (BAL * IR * AIR) - 40000;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        try
        {
            double AMT;
            AMT = (double.Parse(textBox1.Text));

            if (radioButton1.Checked==true)
            {
                do
                {
                const double IR3 = 0.03;
                double BAL, IR, earn;
                int year = 2014;

                AMT = (double.Parse(textBox1.Text));
                IR = IR3;
                year++;
                BAL = InheritanceAmount(AMT, IR);
                earn = InterestEarned(AMT);

                listBox1.Items.Add("You have chosen a 3% inflation rate. Your investment starts at" + AMT.ToString("C") + " and earn 7% a year. You withdraw  $40,000 a year.");
                listBox1.Items.Add("Year" + "\t" + "Interest Earned" + "\t" + "Balance");
                listBox1.Items.Add(year++ + "\t" + earn.ToString("C") + "\t" + BAL.ToString("C"));
                } while (AMT > 40000);
            }
                else if (radioButton2.Checked==true)
                {
                    do
                    {
                    const double IR4 = 0.04;
                    double BAL, IR, earn;
                    int year = 2014;

                    AMT = (double.Parse(textBox1.Text));
                    IR = IR4;
                    year++;
                    BAL = InheritanceAmount(AMT, IR);
                    earn = InterestEarned(AMT);

                    listBox1.Items.Add("You have chosen a 4% inflation rate. Your investment starts at" + AMT.ToString("C") + " and earn 7% a year. You withdraw  $40,000 a year.");
                    listBox1.Items.Add("Year" + "\t" + "Interest Earned" + "\t" + "Balance");
                    listBox1.Items.Add(year++ + "\t" + earn.ToString("C") + "\t" + BAL.ToString("C"));
                    } while (AMT > 40000);
                }
            else
            {                   
                MessageBox.Show("Please select an inflation rate.");                    
            }

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

2 个答案:

答案 0 :(得分:2)

do ... while循环是以AMT的值大于40000为条件的。但是AMT的值仅来自用户(通过文本框),并且永远不会再次更改。所以循环才会永远发生(因为它在你的UI线程上运行,会锁定UI)。您的条件是错误的,或者您需要在循环中更改AMT的值。

答案 1 :(得分:0)

您的while条件基于AMT变量(&gt; 40000)。在do while之前正确初始化该变量。但是,您还要在do while循环中重新初始化该变量,这就是变量永远不会达到使其退出do while循环的条件的原因。

这里要做的第一件事就是将AMT变量设置的行注释回原始值:

do
{
  const double IR3 = 0.03;
  double BAL, IR, earn;
  int year = 2014;

  //Comment the line below
  //AMT = (double.Parse(textBox1.Text));
  IR = IR3;
  year++;
  BAL = InheritanceAmount(AMT, IR);
  earn = InterestEarned(AMT);

  //...

} while (AMT > 40000);

接下来应该做的是根据earn值增加AMT:

AMT += earn;

当AMT原始值<= 0或者兴趣设置为0时,你应该考虑的最后一点是避免无限循环的方法。