C# 显示/数学中的逻辑错误未显示初始结果
我有一个学校人口估计程序,结果不显示第一组结果。这是代码C# 显示/数学中的逻辑错误未显示初始结果,c#,math,return,return-value,C#,Math,Return,Return Value,我有一个学校人口估计程序,结果不显示第一组结果。这是代码 private void Button1_Click(object sender, EventArgs e) { double startingPop; double increasePer; double numDays; const int INTERVAL = 1; if (double.TryParse(textBox1.Text, out startingPop)) {
private void Button1_Click(object sender, EventArgs e)
{
double startingPop;
double increasePer;
double numDays;
const int INTERVAL = 1;
if (double.TryParse(textBox1.Text, out startingPop))
{
if (double.TryParse(textBox2.Text, out increasePer))
{
if (double.TryParse(textBox3.Text, out numDays))
{
for (int i = 1; i <= numDays; i += INTERVAL)
{
startingPop = (startingPop * (increasePer / 100) + startingPop);
Results.Items.Add("After " + i + " days, the amount of organisms is " + startingPop);
}
}
}
}
}
private void Button2_Click(object sender, EventArgs e)
{
this.Close();
}
private void Button3_Click(object sender, EventArgs e)
{
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
Results.Items.Clear();
}
private void按钮1\u单击(对象发送者,事件参数e)
{
双启动POP;
双倍递增器;
双数日;
const int INTERVAL=1;
if(double.TryParse(textBox1.Text,out startingPop))
{
if(double.TryParse(textBox2.Text,out increasePer))
{
if(double.TryParse(textBox3.Text,out numDays))
{
对于(int i=1;i如果我正确理解了您的问题,您的代码应该如下所示:
private void button1_Click(object sender, EventArgs e)
{
double startingPop;
double increasePer;
double numDays;
const int INTERVAL = 1;
if (double.TryParse(textBox1.Text, out startingPop) &&
double.TryParse(textBox2.Text, out increasePer) &&
double.TryParse(textBox3.Text, out numDays))
{
Results.Items.Add("On the first day, the amount of organisms is " + startingPop);
for (int i = 1; i <= numDays; i += INTERVAL)
{
startingPop = (startingPop * (increasePer / 100) + startingPop);
Results.Items.Add("After " + i + " day(s), the amount of organisms is " + startingPop);
}
}
}
现在,假设textBox1
、textBox2
、textBox3
中的值为2、30和5,显示的结果如下:
private void button1_Click(object sender, EventArgs e)
{
double startingPop;
double increasePer;
double numDays;
const int INTERVAL = 1;
if (double.TryParse(textBox1.Text, out startingPop) &&
double.TryParse(textBox2.Text, out increasePer) &&
double.TryParse(textBox3.Text, out numDays))
{
Results.Items.Add("On the first day, the amount of organisms is " + startingPop);
for (int i = 1; i <= numDays; i += INTERVAL)
{
startingPop = (startingPop * (increasePer / 100) + startingPop);
Results.Items.Add("After " + i + " day(s), the amount of organisms is " + startingPop);
}
}
}
文本框的值是什么?看起来您需要做的只是将循环中的两行交换为循环。也就是说,您确定这是一个逻辑错误吗?换句话说,您确定“1天后”,种群仍应与起始种群相同?不,我需要它显示第1天的起始pop,然后显示应用增加后几天的pop。startingPop变量也是估计种群。它应显示如第1天2个生物体第2天2.6个生物体第3天3.38等,它显示为第1天2.6个生物体目前。