display/math 中的逻辑错误未显示初始结果
Logical error in display/math not displaying initial result
我有一个学校人口估算程序,结果不显示第一组结果。这是代码。
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();
}
我希望它显示第 1 天 2 个生物体,而不是显示第一个计算的增加百分比,即(第 1 天 2.6)。我知道这可能非常明显,所以我深表歉意。感谢您的见解。
如果我正确理解你的问题,你的代码应该是这样的:
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);
}
}
}
我对代码所做的修改:
- 使用
&&
运算符将三个 if
语句合并为一个。
- 在增加它之前打印人口值以便下一个显示的值(读取"after 1 day")将是下一个增加的值。
为了让你的结果项在语法上正确,你可以在循环内的第二行加上类似的东西:
string s = (i > 1 ? "s" : string.Empty);
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))
{
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();
}
我希望它显示第 1 天 2 个生物体,而不是显示第一个计算的增加百分比,即(第 1 天 2.6)。我知道这可能非常明显,所以我深表歉意。感谢您的见解。
如果我正确理解你的问题,你的代码应该是这样的:
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);
}
}
}
我对代码所做的修改:
- 使用
&&
运算符将三个if
语句合并为一个。 - 在增加它之前打印人口值以便下一个显示的值(读取"after 1 day")将是下一个增加的值。
为了让你的结果项在语法上正确,你可以在循环内的第二行加上类似的东西:
string s = (i > 1 ? "s" : string.Empty); Results.Items.Add($"After {i} day{s}, the amount of organisms is {startingPop}.");
现在,假设 textBox1
、textBox2
、textBox3
中的值为 2、30 和 5,则显示的结果将如下所示: