Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.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#中实现这样的循环?_C#_.net - Fatal编程技术网

C#如何在C#中实现这样的循环?

C#如何在C#中实现这样的循环?,c#,.net,C#,.net,我想问一下如何使循环像这样: 1 1 2 3 3 6 4 10 5 15 6 21 7 28 控制台中的视图如下所示: 1 1 2 3 3 6 4 10 5 15 6 21 7 28 我已经知道怎么做了 int i; int y = 1; for (i = 1; i <= 7; i++) { Console.WriteLine(i); for (int x = 1; x < 7; x += i) { Console.WriteLine("

我想问一下如何使循环像这样:

1 1
2 3
3 6
4 10
5 15
6 21
7 28
控制台中的视图如下所示:

1 1
2 3
3 6
4 10
5 15
6 21
7 28
我已经知道怎么做了

int i;
int y = 1;

for (i = 1; i <= 7; i++)
{
    Console.WriteLine(i);
    for (int x = 1; x < 7; x += i)
    {
        Console.WriteLine("\t" + x);
    }
}
inti;
int y=1;

对于(i=1;i这是一种猜测,但尝试一下:

int addition = 0;
for (int i = 1; i <= 7; i++)
{
    Console.WriteLine("{0} {1}", i, i+addition);

    addition = i+addition;
}
int加法=0;
对于(inti=1;i
inty=0;

对于(inti=1;i,如果您查看所需的输出。此代码将帮助您获得所需的输出

int addition = 0;
for(int i = 1; i <= 7; i++)
{
    addition += i;
    Console.WriteLine(i + " " + addition);
}
您要打印列车时刻表号码:

为此,您可以使用Linq:

循环实现的替代方案:

  for (int i = 1, a = 1; i <= 7; i++, a += i)
    Console.WriteLine($"{i} {a}");
你可以用这个

      int[] input = new int[] { 1, 2, 3, 4, 5, 6, 7 };
    List<int> output = new List<int>();
    int i;
    int k = 0;
    int y = 0;
    int z = 1;
    for (i = 0; i < 7; i++)
    {
        output.Add(input[i]);
        output.Add(input[i] + y);
        y = output.Last();
        z = output.Count;
        for (int j = k; j < z; j++)
        {
            Console.Write(output[j] + " ");
        }
        k = output.Count;
        Console.WriteLine();
int[]input=newint[]{1,2,3,4,5,6,7};
列表输出=新列表();
int i;
int k=0;
int y=0;
intz=1;
对于(i=0;i<7;i++)
{
输出。添加(输入[i]);
输出相加(输入[i]+y);
y=输出。最后一次();
z=输出。计数;
对于(int j=k;j
您是否为我们发布了一个谜题,让我们找到
1 12 3 3 6 4 10 5 15 6 21 7 28
?您也不清楚为什么希望当前代码做任何不同的事情。请具体说明您的问题-这不应该归结为“向我展示生成此输出的代码”.提示:我想你可以使用嵌套循环,但你不想在该循环中写入任何输出…你只想将数字相加。而且你的嵌套循环不应该每次都添加
I
。我想喜欢第一个视图。我是这个论坛的新手。对不起,我的问题非常糟糕,几乎正确,但为什么总数不是28?我想要left数字总是增加正确的数字,而不是减少太好了!这是我想要的。非常感谢你回答我的问题,先生。非常感谢。上帝保佑你。不客气。我建议你真的尝试一下,这样你开始以算法的形式思考,如果你已经做了100次,你将对这些问题有一个眼睛,并正确地看到解决方案但是首先你必须努力思考问题的每一步。
  string report = string.Join(Environment.NewLine, Enumerable
    .Range(1, 7)
    .Select(i => $"{i} {i * (i + 1) / 2}"));

  Console.Write(report);
  for (int i = 1, a = 1; i <= 7; i++, a += i)
    Console.WriteLine($"{i} {a}");
1 1
2 3
3 6
4 10
5 15
6 21
7 28
      int[] input = new int[] { 1, 2, 3, 4, 5, 6, 7 };
    List<int> output = new List<int>();
    int i;
    int k = 0;
    int y = 0;
    int z = 1;
    for (i = 0; i < 7; i++)
    {
        output.Add(input[i]);
        output.Add(input[i] + y);
        y = output.Last();
        z = output.Count;
        for (int j = k; j < z; j++)
        {
            Console.Write(output[j] + " ");
        }
        k = output.Count;
        Console.WriteLine();