C# 循环打印顺序中的最后一个数字
如何打印序列中的最后一个数字以及它在for循环中运行的次数C# 循环打印顺序中的最后一个数字,c#,C#,如何打印序列中的最后一个数字以及它在for循环中运行的次数 int numOne = 50; for (int i = 0; i < numOne; i++) { Console.WriteLine(i); } int numOne=50; for(int i=0;iitem==lastItem)); 一些名为TheList的列表,您使用TheList[TheList.Length
int numOne = 50;
for (int i = 0; i < numOne; i++)
{
Console.WriteLine(i);
}
int numOne=50;
for(int i=0;ivar list=新列表{1,2,3,6,10,1,5,1};
var lastItem=list.Last();
Console.WriteLine(“{0}{1}”,lastItem,list.Count(item=>item==lastItem));
一些名为TheList
的列表,您使用TheList[TheList.Length-1]
获取最后一项。为什么您不能直接获取最后一个数字,而不使用循环?这样会更有效率。如果你描述一下你的输入数据是什么样子的,以及你是如何得到它的,那么我会给你你需要的答案;for(inti=0;i
如何打印for循环中的最后一个数字,类似于list方法。作为替代方案,您可以将for循环添加到列表中吗@Serega@Zuoanqh@Rand Random@AustinWBryanSorry,Carlos-但是你的问题很难理解。我不知道你想要什么。也许你可以编辑你的问题,用一个你想要的输出的例子。@AustinWBryan-你编辑的太多了,做一个新的answer@Serega如何自动填充列表的内容。例如,For循环将打印项目列表,直到满足条件为止。可以使用for循环内容填充列表。
var list = new List<int> { 1, 2, 3, 6, 10, 1, 5, 1 };
var lastItem = list.Last();
Console.WriteLine("{0} {1}", lastItem, list.Count(item => item == lastItem));