简体   繁体   English

如何在 0-100 的每一行上得到 3 个数字?

[英]How do I get 3 numbers on each line from 0-100?

I need to write numbers from 0-100 on the console, like this:我需要在控制台上写 0-100 之间的数字,如下所示:

0,1,2 0,1,2

1,2,3 1,2,3

2,3,4 2,3,4

3,4,5 3、4、5

etc.等等。

I really can't seem to figure this out.我似乎真的无法弄清楚这一点。 Any pointers would be nice!任何指针都会很好!

It's quite easy actually, something like:其实很简单,比如:

for(int i = 0; i < 100; i++) {
    Console.WriteLine(string.Format("{0},{1},{2}", i, i+1, i+2));
}

It will goes from 0 to 101 because it always prints 3 numbers so the last iteration would be 99,100 otherwise.它会从 0 到 101,因为它总是打印 3 个数字,否则最后一次迭代将是 99,100。 If you want it that way just edit i < 100 with i < 99 .如果你想要那样,只需用i < 99编辑i < 100

A LINQ way of achieving it:实现它的 LINQ 方式:

Enumerable.Range(2, 99).ToList()
        .ForEach(x => Console.WriteLine($"{x - 2},{x - 1},{x}"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM