我有"i"这是一个整数变量,我想做一个循环,将"i"从40000增加到90000,每次增加1000.每个结果都将出现在ComboBox中.
示例:40000 - 41000 - 42000 - 43000 - ... - 88000 - 89000 - 90000
我的代码如下:
var i:integer;
begin
for i:= 40000 to 90000 do
begin
ComboBox1.AddItem(IntToStr(i), nil); //until here the code works
Inc(i, 1000);
end;
Run Code Online (Sandbox Code Playgroud)
你有什么建议吗?
Ken*_*ite 10
@ AndreasRejbrand解决方案的替代方案是while循环:
i := 40000;
while i <= 90000 do
begin
ComboBox1.AddItem(IntToStr(i), nil);
Inc(i, 1000);
end;
Run Code Online (Sandbox Code Playgroud)
或者"重复":
i := 40000;
repeat
ComboBox1.AddItem(IntToStr(i), nil);
Inc(i, 1000);
until i > 90000;
Run Code Online (Sandbox Code Playgroud)
你不能在循环中改变for循环变量.
你想要的是这个:
for i := 0 to 50 do
ComboBox1.AddItem(IntToStr(40000 + 1000 * i), nil)
Run Code Online (Sandbox Code Playgroud)
但!这是相当低效的.你应该考虑一下
ComboBox1.Items.BeginUpdate;
for i := 0 to 50 do
ComboBox1.Items.Add(IntToStr(40000 + 1000 * i));
ComboBox1.Items.EndUpdate;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15173 次 |
| 最近记录: |