Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 循环打印顺序中的最后一个数字_C# - Fatal编程技术网

C# 循环打印顺序中的最后一个数字

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

如何打印序列中的最后一个数字以及它在for循环中运行的次数

  int numOne = 50;
            for (int i = 0; i < numOne; i++)
            {
                Console.WriteLine(i);
            }
int numOne=50;
for(int i=0;i
var 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));