using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace project5
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }        private void btnCompute_Click(object sender, EventArgs e)
        {
            float p;
            float r;
            double total;
            int n = 1;
            p = float.Parse(txtPrincipal.Text);
            r = float.Parse(txtAunnalInteretRate.Text);
            total = p * (Math.Pow((1 + r), n) - 1) * (1 + r) / r;
            if (total < 1000000)
            {
                n = n + 1;
                total = p * (Math.Pow((1 + r), n) - 1) * (1 + r) / r;
            }
            else
            {
                txtYear.Text = n.ToString();
            }
        }
    }
}

解决方案 »

  1.   

    total < 1000000 的话,你的代码没有更新界面的操作,看上去的确“没有出结果”你需要做:要么给这个判断分支之后增加更新界面的代码;要么检查代码,为什么没有按照预期执行 total >= 1000000 的分支。
      

  2.   


    //这样看看
      if (total < 1000000)
      {
        n = n + 1;
        total = p * (Math.Pow((1 + r), n) - 1) * (1 + r) / r;
        txtYear.Text = total.ToString(); //加上这句再看看
      }
      else
      {
        txtYear.Text = n.ToString();
      }
      

  3.   

    total<1000000这if 出现判断有问题,
      

  4.   


     if (total < 1000000)
                {
                    n = n + 1;
                    total = p * (Math.Pow((1 + r), n) - 1) * (1 + r) / r;
                    textBox3.Text = total.ToString();
                }
                else
                {
                    textBox3.Text = n.ToString();
                }