【发布时间】:2015-06-16 00:37:49
【问题描述】:
我尝试使用 do-while 循环,但在我输入输入并启动按钮单击事件后它什么也没做。它应该计算出随后所有年份的数量和清单,直到它
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);
}
【问题讨论】:
-
在调试器中单步执行代码是否会产生任何见解?
-
顺便说一句,可以通过将其从
if块中取出来避免循环重复 -
另外,由于
RadioButton的Checked属性是布尔值,我会do away with the explicit comparison。 -
这意味着我可以像 (radioButton1.Checked) 一样写它
标签: c# loops calculator do-while