viernes, 21 de octubre de 2016

Programación en C#: Módulo 02: Métodos y Manejo de Excepciones: Ejemplo

Métodos:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Demo5
{
    class Program
    {
        static bool EsPar(int Numero)
        {
            bool Bandera = false;
            int Residuo = 0;
            Residuo = Numero % 2;

            if (Residuo == 0)
                Bandera = true;
            else
                Bandera = false;

            return Bandera;
        }

        static bool EsPar(string sNumero)
        {
            bool EsNumero = false; int Num = 0;
            bool bRetorno = false;

            EsNumero = int.TryParse(sNumero, out Num);
            if (EsNumero)
            { bRetorno = EsPar(Num); }
            else
            { bRetorno = false; }

            return bRetorno;
        }

        static void AnalizarNumero()
        {
            int N = 0; string sNum = "";
            Console.WriteLine("Ingrese un número entero?");
            sNum = Console.ReadLine();

            N = int.Parse(sNum);
            if(EsPar(N))
            { Console.WriteLine("{0} Es Par", N); }
            else
            { Console.WriteLine("{0} Es ImPar", N); }
        }

        static void AnalizarNumero2()
        {
            string sNum = "";

            Console.WriteLine("Ingrese un número entero?");
            sNum = Console.ReadLine();

            if ( EsPar(sNum) )
            { Console.WriteLine("{0} Es Par", sNum); }
            else
            { Console.WriteLine("{0} Es ImPar", sNum); }
        }

        static bool EsBisiesto(int Anio, out string Mensaje)
        {
            bool bValor = false; int Residuo = 0;
            string MiMensaje = ""; int Res100 = 0;

            Residuo = Anio % 4;
            if (Residuo==0)
            {
                Res100 = Anio % 100;
                if (Res100 == 0)
                {
                    bValor = false;
                    MiMensaje = "No es un Año Bisiesto";
                }
                else
                {
                    bValor = true;
                    MiMensaje = "Es un Año Bisiesto";
                }
            }
            else
            {
                bValor = false;
                MiMensaje = "No es un Año Bisiesto";
            }

            //Retorna Valores
            Mensaje = MiMensaje;
            return bValor;
        }

        static void AnioBisiesto()
        {   //Variables
            string sNum = "", sMensaje="";int Anio = 0;
            //Ingreso de Datos
            Console.WriteLine("Ingrese un año?");
            sNum = Console.ReadLine();
            //Procesar Datos
            Anio = int.Parse(sNum);
            if (EsBisiesto(Anio, out sMensaje))
            { Console.WriteLine("{0:D} {1}", Anio,sMensaje); }
            else
            { Console.WriteLine("{0:D} {1}", Anio, sMensaje); }
        }

        static void Main(string[] args)
        {
            //AnalizarNumero();
            //AnalizarNumero2();
            AnioBisiesto();

            Console.ReadKey();
        }

    }

}


No hay comentarios.:

Publicar un comentario