domingo, 28 de julho de 2019

C# - Array unidimensional de string

Neste exemplo estamos inicializando
um array unidimensional de string,
em sua própria declaração.
Neste caso não é necessário especificar
o comprimento, já que o C# rastreia o total de
elementos dos itens inicializados automaticamente.
O código é indicado aos iniciantes em C#.


Veja abaixo uma imagem do programa em execução:



Veja abaixo o código do programa:


//Array unidimensional de string final
using System;
using System.Collections;

class Arr {
    /*============================================================*/
    void Informe ( ) {
        Console.BackgroundColor = ConsoleColor.White;
        Console.SetCursorPosition ( 20, 20 );
        Console.ForegroundColor = ConsoleColor.Red;
        Console.Write ( "Por: " );
        Console.SetCursorPosition ( 25, 20 );
        Console.ForegroundColor = ConsoleColor.Blue;
        Console.Write ( "Samuel Lima" );
        Console.SetCursorPosition ( 20, 21 );
        Console.ForegroundColor = ConsoleColor.Black;
        Console.Write ( "sa_sp10@hotmail.com" );
        Console.SetCursorPosition ( 30, 23 );
        Console.ForegroundColor = ConsoleColor.Red;
        Console.Write ( "MUITO OBRIGADO" );
    }
    /*============================================================*/
    void Moldura ( int tam_lin_ini, int tam_lin_fim, int tam_ini_col, int tam_fim_col ) {
        int i, c;
        for ( i = tam_lin_ini; i < tam_lin_fim; i++ ) {
            for ( c = tam_ini_col; c < tam_fim_col; c++ ) {  
                Console.SetCursorPosition ( c, i );              
                Console.BackgroundColor = ConsoleColor.White;
                Console.Write ( " " );             
            }
        }
    }
    /*============================================================*/
    void Barra ( int lin, int col ) {
        int i, j;
        for ( i = 0; i < lin; i++ )
            for ( j = 0; j < col; j++ ) {
                Console.SetCursorPosition ( i, j );
                Console.BackgroundColor = ConsoleColor.Magenta;
                Console.Write ( " " );
            }
    }
    /*============================================================*/
    void Ini_Array ( ) {
        int i;
        string [ ] nomes = new string [ ] {
              "Q O B G C O H O P P N A Y G B D F R H O\n",
              "O C V W M L W A U P W G K I S J G A U A\n",
              "O I I J V I P I D F Q N I O K Z Z Z Y C\n",
              "T T W S S C P A X B L I G S O R V I O A\n",
              "N S O J E A M B I N D T H T Y H F L O E\n",
              "G I H T J B M K J Z I A A G O K I A M T\n",
              "R L T P N W T Q Y F Y C O T Z W S B H E\n",
              "X A V C L E R G V W C H N G A J J A I C\n",
              "A B Z S L F M I K T H E I V O U K G L A\n",
              "Z A J I Z E A A R I M S A R K P N D F C\n",
              "R C Y B L B T W H A N A E E H C Z N L L\n",
              "R J N E D A V N H N L D J N X Z A F E B\n",
              "C D A U C C N L E O I V X J Q T S F H I\n",
              "L A T G L O U A R C J P O E V J U V O X\n",
              "M O J I Q B L A B M A W S U D W G R F D\n",
              "Y R X U J C V S T X B G K Y W J B M G I\n"};
        //Posiciona os textos no console
        Console.SetCursorPosition (3, 3 );
        for ( i = 0; i < 16; i++ ) {
            Console.Write ( "\t" );
            Console.ForegroundColor = ConsoleColor.Red;
            Console.Write (  "  " + i );
            Console.Write ( "\t" );
            Console.ForegroundColor = ConsoleColor.Black;
            Console.Write ( nomes [ i ] );
        }
        Arr array = new Arr ( );
        array.Barra ( 2, 24 );
    }
    /*============================================================*/
    public static void Main ( ) {
        Console.Title = ( "ARRAY UNIDIMENSIONAL DE STRING" );
        Arr array = new Arr ( );
        array.Moldura ( 1, 24, 2, 68 );
        array.Ini_Array ( );
        Console.SetCursorPosition ( 20, 1 );
        Console.BackgroundColor = ConsoleColor.White;
        Console.ForegroundColor = ConsoleColor.Red;
        Console.Write ( "ARRAY UNIDIMENSIONAL DE STRING" );
        array.Informe ( );
        //Segura a execução do programa
        Console.ReadKey ( );
    }
}
/*============================================================*/





Nenhum comentário:

Postar um comentário