如何计算循环执行的次数 [英] How to calculate how many times loop is executing

查看:1360
本文介绍了如何计算循环执行的次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int Totalcount =?



foreach(列表中的字符串s)

{

count ++; < br $>
}





我怎么知道我的总数是多少?

int Totalcount=?

foreach (string s in List)
{
count++;
}


how could i know how much my total count is?

推荐答案

int Totalcount=0;
 
foreach (string s in List)
{
Totalcount++;
}





或者您可以计算列表中更有效的项目(列表中有一个名为<的属性code> Count 返回列表中项目的计数),因为列表中的每个项目的每个循环都会进行。



欢呼,

Marco



Or you could count the items in the list which would be more efficient (The list has a property called Count which returns the count of the items in the list), since the for each loop goes through for every item in the list.

cheers,
Marco


尝试:
int totalCount = 0;
foreach (string s in List)
    {
    totalCount++;
    }
Console.WriteLine(totalCount);

或者,你可以使用:

Alternatively, you could just use:

Console.WriteLine(List.Count);


int Totalcount=0;
foreach (string s in List)
{
 Totalcount++;
}
Label1.Text = "For loop has been executed " + Totalcount.ToString() +  " times";



Happy Coding!

:)


Happy Coding!
:)


这篇关于如何计算循环执行的次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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