在循环中增加一个整数变量 [英] Increment an integer variable in a loop

查看:212
本文介绍了在循环中增加一个整数变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个整数变量的i,我想做一个循环,将i从40000增加到90000,每次增加1000。每个结果将显示在组合框中。



示例:40000 - 41000 - 42000 - 43000 - ... - 88000 - 89000 - 90000



我的代码如下:

  var i:integer; 
begin
for i:= 40000到90000 do
begin
ComboBox1.AddItem(IntToStr(i),nil); //直到这里代码工作
Inc(i,1000);
结束

你有什么建议吗?



你想要的是这样的:

  for i:= 0 to 50 do 
ComboBox1.AddItem(IntToStr(40000 + 1000 * i),nil)

但是!这是相当低效的。你应该考虑

  ComboBox1.Items.BeginUpdate; 
for i:= 0 to 50 do
ComboBox1.Items.Add(IntToStr(40000 + 1000 * i));
ComboBox1.Items.EndUpdate;


I have "i" that is an integer variable and I would like to do a loop that increments the "i" from 40000 to 90000 adding 1000 each time. Each Result will appear in a ComboBox.

Example: 40000 - 41000 - 42000 - 43000 - ... - 88000 - 89000 - 90000

My code is the following:

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;

Do you have any suggestions?

解决方案

You cannot alter the for loop variable inside the loop.

What you want is this:

for i := 0 to 50 do
  ComboBox1.AddItem(IntToStr(40000 + 1000 * i), nil)

But! This is rather inefficient. You should consider

ComboBox1.Items.BeginUpdate;
for i := 0 to 50 do
  ComboBox1.Items.Add(IntToStr(40000 + 1000 * i));
ComboBox1.Items.EndUpdate;

这篇关于在循环中增加一个整数变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆