C# 为什么循环在51之后继续?
我有一个按钮4点击事件:C# 为什么循环在51之后继续?,c#,winforms,C#,Winforms,我有一个按钮4点击事件: private void button4_Click(object sender, EventArgs e) { string mainpath = Path.Combine(@"c:\temp\newimages", "Changed_Resolution_By_" + numeric.ToString()); Directory.CreateDirectory(ma
private void button4_Click(object sender, EventArgs e)
{
string mainpath = Path.Combine(@"c:\temp\newimages",
"Changed_Resolution_By_" + numeric.ToString());
Directory.CreateDirectory(mainpath);
Image img = Image.FromFile(previewFileName);
int width = img.Width;
int height = img.Height;
double res = width / numeric;
dirsnumbers = (int)Math.Floor(res);
for (int i = 0; i <= dirsnumbers; i++)
{
width = width - numeric;
height = height - numeric;
path = Path.Combine(mainpath,
String.Concat("SecondProcess_", DateTime.Now.ToString("MMddyyyy-HHmmss")) +
"-" + "Width = " + (width - numeric) + " Height = " + (height - numeric));
if ((width - numeric) > 0)
{
Directory.CreateDirectory(path);
}
}
backgroundWorker2.RunWorkerAsync();
}
现在我在dirsnumbers上循环,并在行上使用了一个断点:
width = width - numeric;
所以,如果宽度是512数字10,那么第一个itertion宽度将是502
最后宽度是2。但是它做了另一个迭代,得到-10,所以宽度=-8
我使用了一个断点,我看到在itertion 51上宽度是2,那么为什么它要做另一个itertion并使宽度为-8呢
编辑**
这是有效的:
for (int i = 0; i < dirsnumbers; i++)
{
width = width - numeric;
height = height - numeric;
path = Path.Combine(mainpath,
String.Concat("SecondProcess_", DateTime.Now.ToString("MMddyyyy-HHmmss")) +
"-" + "Width = " + (width) + " Height = " + (height));
Directory.CreateDirectory(path);
}
我在这一行做了(宽度-数字)和(高度-数字),我在这一行前面已经做了。一旦我删除,只留下宽度和高度,它的工作。不多了-8。
甚至当我从“我您的循环运行了
52次,因为您使用您的循环运行了52次,因为您使用了0到51=52次迭代。尝试宽度为512数字10,然后第一个i插入宽度将为492
嗯。。。512-10=502…好的,我找到了解决方案,至少它现在可以工作了。0到51=52次迭代。尝试宽度为512数字10,然后第一个i插入宽度将为492
嗯。。。512-10=502….好的,我找到了解决方案,至少它现在起作用了。
for (int i = 0; i < dirsnumbers; i++)
{
width = width - numeric;
height = height - numeric;
path = Path.Combine(mainpath,
String.Concat("SecondProcess_", DateTime.Now.ToString("MMddyyyy-HHmmss")) +
"-" + "Width = " + (width) + " Height = " + (height));
Directory.CreateDirectory(path);
}
path = Path.Combine(mainpath,
String.Concat("SecondProcess_", DateTime.Now.ToString("MMddyyyy-HHmmss")) +
"-" + "Width = " + (width - numeric) + " Height = " + (height - numeric));