quinta-feira, 13 de dezembro de 2018

C# - pares e ímpares em array de inteiros

Aqui está um exemplo de como obter números
pares e ímpares num array de inteiros,
que foram gerados por um contador com laço for()
Para testar o programa, basta criar um novo
projeto selecionando:
Aplicativo do Windows Form(.Net Framework),
apaga o código gerado no arquivo Program.cs
substituindo por este sugerido.

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


Veja abaixo o código do programa:


using System;
using System.Drawing;
using System.Windows.Forms;

namespace keypress {

    public partial class Form1: Form {
        System.Text.StringBuilder str = new System.Text.StringBuilder ( );
        System.Text.StringBuilder str_1 = new System.Text.StringBuilder ( );
        /*============================================================*/
        public void Ini_Array ( ) {
           int [ ] arr = new int [ 100 ];
            int a = 0, i = 0;
            for ( i = 1; i <= arr.Length; i++ ) {
                arr [ a ] = i;
                a++;
            }
            for ( i = 0; i < arr.Length; i++ ) {
                if ( i % 10 == 0 ) {
                    str.Append ( "\n" );
                    str_1.Append ( "\n" );
                }
                if ( i % 2 == 1 ) {
                    if ( i >= 0 && i < 9 )
                        str.Append ( "0" );
                    str.Append ( arr [ i ] + "     " );
                } else {
                    if ( i >= 0 && i < 9 )
                        str_1.Append ( "0" );
                    str_1.Append ( arr [ i ] + "     " );
                }
            }
        }
        /*============================================================*/
        protected override void OnPaint ( PaintEventArgs e ) {
            this.Size = new System.Drawing.Size ( 600, 300 );
            this.Text = "C# - PARES E ÍMPARES EM ARRAY";
            this.BackColor = Color.LightBlue;
            Graphics dc = e.Graphics;
            Pen BluePen = new Pen ( Color.Red, 10 );
            dc.DrawRectangle ( BluePen, 5, 5, 575, 250 );
            Font myFont = new System.Drawing.Font ( "Consolas", 12,
                FontStyle.Italic );
            Brush myBrush = new SolidBrush ( Color.Black );
            Brush myBrush_1 = new SolidBrush ( Color.Red );
            dc.DrawString ( "C# - PARES E ÍMPARES EM ARRAY",
                myFont, myBrush_1, 170, 15 );
            dc.DrawString ( "" + str, myFont, myBrush, 180, 30 );
            dc.DrawString ( "" + str_1, myFont, myBrush_1, 150, 30 );
        }
        /*============================================================*/
        public Form1 ( ) {
            Ini_Array ( );
        }
    }
    /*============================================================*/
    static class Program {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main ( ) {
            Application.EnableVisualStyles ( );
            Application.SetCompatibleTextRenderingDefault ( false );
            Application.Run ( new Form1 ( ) );
        }
    }
}

Nenhum comentário:

Postar um comentário