How to use Multiple Console.Write with for loop?

FranK Duc 121 Reputation points
2021-07-29T13:42:35.463+00:00

Hello,

I want to get my value output on the different lines. How do i do that?

using System;

public class _Example
{
    public static void Main()
    {
        for (int counter = 1; counter <= 12; counter++)
        {

           Console.Write(" "+counter);


            if (counter < 5)
    {


                  Console.Write("  " +counter);

        continue;


    }




        }
    }
}

I am getting that output:

11 22 33 44 5 6 7 8 9 10 11 12

When it should be:

1 2 3 4 5 6 7 8 9 10 11 12
1 2 3 4 on a second line

Thank you

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,650 questions
{count} votes

Accepted answer
  1. Jack J Jun 24,496 Reputation points Microsoft Vendor
    2021-08-10T08:23:59.507+00:00

    @FranK Duc , you could try the following code to seperate the output on 2 different lines.

    Code:

                    List<int> list = new List<int>();  
                    for (int i = 1; i < 13; i++)  
                    {  
                        list.Add(i);  
                    }  
                    list.Take(4).ToList().ForEach(i => Console.Write(" "+i));  
                    Console.WriteLine();  
                    list.ForEach(i => Console.Write(" "+i));  
                    Console.ReadKey();  
    

    Result:

    121914-image.png

    0 comments No comments

4 additional answers

Sort by: Most helpful
  1. Castorix31 83,206 Reputation points
    2021-07-29T14:40:40.947+00:00

    You could re-position the Cursor
    To be improved, a quick test :

    int nPos = 0;
    for (int counter = 1; counter <= 12; counter++)
    {
        nPos += counter.ToString().Length + 1;
        Console.Write(" " + counter);
        Console.WriteLine();
        if (counter < 5)
        {
            Console.SetCursorPosition((counter - 1) * 2, Console.CursorTop);
            Console.Write(" " + counter);
            Console.SetCursorPosition(nPos, Console.CursorTop - 1);
            continue;
        }
        else
        {
            Console.SetCursorPosition(nPos, Console.CursorTop - 1);
        }   
    }
    
    1 person found this answer helpful.
    0 comments No comments

  2. Sam of Simple Samples 5,531 Reputation points
    2021-07-29T16:06:22.827+00:00
    StringBuilder sb1 = new StringBuilder();
    StringBuilder sb2 = new StringBuilder();
    for (int counter = 1; counter <= 12; counter++)
    {
        sb1.Append($" {counter}");
        if (counter < 5)
        {
            sb2.Append($" {counter}");
        }
    }
    Console.WriteLine(sb1.ToString());
    Console.WriteLine(sb2.ToString());
    
    0 comments No comments

  3. FranK Duc 121 Reputation points
    2021-07-29T16:08:57.97+00:00

    The loop is fine.

    I just want to know how to seperate the output on 2 different lines.

    The second Console.Write overlap the first one, thats why data comes out on the same line.

    0 comments No comments

  4. FranK Duc 121 Reputation points
    2021-08-10T11:31:19.487+00:00

    Hello Jack,

    Its one complicated way to do it.

    There is that solution also: Thanks

    using System;
    
    public class Example
    {
        public static void Main()
        {
            string firstLine = "", secondLine = "";
    
            for (int counter = 1; counter <= 12; counter++)
            {
                firstLine += " " + counter.ToString();
    
                if (counter < 5)
                {
                    secondLine += " " + counter.ToString();
    
                    continue;
                }
            }
    
            Console.WriteLine(firstLine);
            Console.WriteLine(secondLine);
        }
    }
    
    0 comments No comments