Format the Array view in C#?

Asked

Viewed 80 times

4

I have a string array.

string[] nomes =
{
    "Maria",
    "João",
    "Francisco",
    "José",
    "Antonio",
    "Osvaldo",
    "Francimar",
    "Cirineu"
};

I would like to display them exactly like this on the Console

Maria   João    Francisco José
Antonio Osvaldo Francimar Cirineu

1 answer

2

Here is the code, and its operation explained in the comments

using System;

public class Program
{
    public static void Main()
    {
        string[] nomes =
        {
            "Maria",
            "João",
            "Francisco",
            "José",
            "Antonio",
            "Osvaldo",
            "Francimar",
            "Cirineu"
        };

        //Pecorre todos os nomes
        for (int key = 0; key < nomes.Length; ++key){

            //Escreve o nome
            Console.Write(nomes[key]);

            //Acrescenta espaço na frente do nome par ao alinhamento
            for (int i = 0; i <= 10 - nomes[key].Length; ++i){
                Console.Write(" ");
            }

            //Faz a quebra no quarto nome
            if((key + 1) % 4 == 0){
                Console.Write("\n");
            }
        }
    }
}

Browser other questions tagged

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