How to break lines (console mode)?

Asked

Viewed 11,603 times

2

How I break lines in C#, as if I put two commands Write the console will print them side by side, as I do to print on the bottom line.

Ps: Code below

        var A1 = Console.ReadLine();
        var A2 = Console.ReadLine();
        var A3 = Console.ReadLine();
        var A4 = Console.ReadLine();
        var A5 = Console.ReadLine();
        var A6 = Console.ReadLine();
        var A7 = Console.ReadLine();
        var A8 = Console.ReadLine();
        Console.Write("Segunda :{0}, {1}, {2}, {3},", A1, A8, A4, A5);
        Console.Write("Terça :{0}, {1}, {2}, {3},", A2, A7, A3, A6);
        Console.ReadKey();

2 answers

10

Use Writeline

Console.WriteLine("Segunda :{0}, {1}, {2}, {3},", A1, A8, A4, A5);
Console.WriteLine("Terça :{0}, {1}, {2}, {3},", A2, A7, A3, A6);

7


You have basically three techniques.

One is to use the method itself to skip the line. Most of the time it is the best solution. Just use the WriteLine(), in its various variations.

WriteLine($"Segunda: {A1}, {A8}, {A4}, {3},");
WriteLine($"Terça: {A2}, {A7}, {A3}, {A6},");

May even just skip the line:

WriteLine();

The other technique that can avoid calling this method is to put a control character in the text telling you to skip the line. This character is the \n. It will be replaced by line break.

Write($"Segunda: {A1}, {A8}, {A4}, {3},\n");
Write($"Terça: {A2}, {A7}, {A3}, {A6}\n");

Or where it makes the most sense:

Write($"Segunda: {A1}, {A8}, {A4}, {3},\nTerça: {A2}, {A7}, {A3}, {A6}\n");

It is important to note that this should not be abused. In this example the ideal would be to use another method. It is more readable.

The other technique is to skip the line in the code itself, for this it is necessary to indicate that the text will be raw, that is, it should be used the way it is in the code. This is achieved with the Verbatim indicator (@):

Write($@"Segunda: {A1}, {A8}, {A4}, {3},
Terça: {A2}, {A7}, {A3}, {A6}\n");

See what I used interpolation of string to make the code more fluid.

Also imported the static class not to keep typing Console all the time.

using static System.Console;

public class Program {
    public static void Main() {
        var A1 = ReadLine();
        var A2 = ReadLine();
        var A3 = ReadLine();
        var A4 = ReadLine();
        var A5 = ReadLine();
        var A6 = ReadLine();
        var A7 = ReadLine();
        var A8 = ReadLine();
        WriteLine($"Segunda: {A1}, {A8}, {A4}, {3},");
        WriteLine($"Terça: {A2}, {A7}, {A3}, {A6},");
        Write($"Segunda: {A1}, {A8}, {A4}, {3},");
        WriteLine();
        Write($"Terça: {A2}, {A7}, {A3}, {A6},");
        Write($"Segunda: {A1}, {A8}, {A4}, {3},\nTerça: {A2}, {A7}, {A3}, {A6}\n");
        Write($@"Segunda: {A1}, {A8}, {A4}, {3},
    Terça: {A2}, {A7}, {A3}, {A6}\n");
    }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.