How to break lines (console mode)?

2

As I break lines in C #, type if I for two commands Write the console will print them side by side, how do I 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();
    
asked by anonymous 16.02.2017 / 09:27

2 answers

8

You basically have three techniques.

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

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

You can even just skip the line :

WriteLine();

The other technique that can avoid calling this method is to place a control character in the text telling you to skip the line. This character is \n . It will be replaced by the 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's 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 done with the verbatim flag ( @ ) :

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

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

Also I imported the static class to not be 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");

    }
}

See working on .NET Fiddle . And at Coding Ground . Also put it on GitHub for future reference .

    
16.02.2017 / 12:58
8

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);
    
16.02.2017 / 09:34