Metziop

Untitled

Jun 10th, 2022
1,186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.19 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5.  
  6. namespace DPRN2U3A2
  7. {
  8.     public class DPRN2U3A2
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.  
  13.             try
  14.             {
  15.                 Console.WriteLine("Por favor ingrese su fecha de nacimiento");
  16.                 int yof = Convert.ToInt32(Console.ReadLine());
  17.                 if (Enumerable.Range(1980, 1995).Contains(yof))
  18.                 {
  19.                     throw new YoBFilter("No se puede aplicar la vacuna "); //filtro de excepcion personalizado
  20.                 }
  21.                 Console.WriteLine("Puede recibir la vacuna");
  22.                 var lista = new List<string>();
  23.                 List<string> variantesCovid = new List<string>();
  24.                 variantesCovid.InsertRange(0, new string[] { "alfa", "beta", "gama", "delta", "omicron", "psi" });
  25.                 int[] enteros4 = { 1, 6, 98, 12, 15, 18, 21 };
  26.                 int[] enteros5 = new int[6];
  27.                 int[] valores = null;
  28.                 int[] dividendos = { 20, 7, 0 };
  29.                 Console.WriteLine("Número de elementos en la lista:{0}", lista.Count);
  30.                 try { Console.WriteLine("El primer elemento de la lista es: {0}", lista[0]); }
  31.                 catch (ArgumentOutOfRangeException ex) //handling argument out of range exception
  32.                 {
  33.                     Console.WriteLine(ex.Message);
  34.                 }
  35.  
  36.                 for (int control = 0; control <= variantesCovid.Count; control++)
  37.                
  38.  
  39.                     try { Console.WriteLine("'{0}' ", variantesCovid[control]); }
  40.                     catch (ArgumentOutOfRangeException ex)//handling argument out of range exception
  41.                     {
  42.                         Console.WriteLine(ex.Message);
  43.                     }
  44.  
  45.                     try
  46.                     {
  47.                         enteros5[enteros4.Length - 1] = enteros4[enteros4.Length - 1];
  48.                     }
  49.                     catch (IndexOutOfRangeException ex)//handling index out of range exception
  50.                     {
  51.                         Console.WriteLine(ex.Message);
  52.                     }
  53.                     try
  54.                     {
  55.                         for (int i = 0; i <= 9; i++)
  56.                             valores[i] = i * 2;
  57.                     }
  58.                     catch (NullReferenceException ex)
  59.                     {
  60.                         Console.WriteLine(ex.Message);
  61.                     }
  62.                     try
  63.                     {
  64.                         foreach (var values in valores)
  65.                             Console.WriteLine(values);
  66.                     }
  67.                     catch (NullReferenceException ex)
  68.                     {
  69.                         Console.WriteLine(ex.Message);
  70.  
  71.                     }
  72.  
  73.                     foreach (var value in dividendos)
  74.                     {
  75.                         try
  76.                         {
  77.                             Console.WriteLine("{0} dividido entr 2 es {1}", value, DivideEntreDos(value));
  78.                         }
  79.                         catch (ArgumentException ex)
  80.                         {
  81.                             Console.WriteLine(ex.Message);
  82.                         }
  83.                     }
  84.  
  85.                     Console.WriteLine(Divide(1, 0));
  86.                
  87.             }
  88.             catch (YoBFilter ex) { //excepcion perzonalizada
  89.                 Console.WriteLine(ex.Message);
  90.             }
  91.             Console.WriteLine("Presione una tecla para continuar....");
  92.             Console.ReadKey();
  93.         }
  94.         static int DivideEntreDos(int num) {
  95.             if (!((num % 2) == 0))
  96.             {
  97.                 throw new ArgumentException(String.Format("{0} no es mumero par", num));//excepcion si el numero no es divisibl entre 2
  98.             }
  99.            
  100.             return num / 2;
  101.         }
  102.         static double Divide(double dividendo , double divisor) {
  103.             double resultado = 0;
  104.             try {resultado = dividendo / divisor; }
  105.             catch (DivideByZeroException ex) {
  106.                 Console.WriteLine(ex.Message);
  107.             }
  108.  
  109.             return resultado;
  110.         }
  111.        
  112.     }
Advertisement
Add Comment
Please, Sign In to add comment