private static void Exercise20() { ConsoleAppPrintUtil.PrintTextWithLineSeparator("Ejercicio 29:"); var foundNumber = false; var breakCondition = false; var random = new Random(); var numberToFound = random.Next(1, 100); do { var number = ConsoleAppPrintUtil.PrintQuestionWithIntPositiveOrNegativeAnswerRecursive("Introduzca un número:"); if (number < 0) { breakCondition = true; } else if (number < numberToFound) { ConsoleAppPrintUtil.PrintText($"Lo siento, pero el número está por encima del valor introducido."); } else if (number > numberToFound) { ConsoleAppPrintUtil.PrintText($"Lo siento, pero el número está por debajo del valor introducido."); } else { foundNumber = true; } } while (!foundNumber && !breakCondition); ConsoleAppPrintUtil.PrintText(GetExercise20Text(foundNumber)); }
private static void Exercise2() { ConsoleAppPrintUtil.PrintTextWithLineSeparator("Ejercicio 2:"); try { var questions = new string[] { "Introduce un número de día entre 1 y 365: " }; var numberOfDays = ConsoleAppPrintUtil.PrintQuestionWithIntPositiveOrNegativeAnswerRecursive(questions[0]); if (numberOfDays < 1 || numberOfDays > 365) { throw new ArgumentOutOfRangeException($"Día fuera de intervalo"); } var daysInMonths = new int[] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; var monthNumber = Exercise2GetMonthNumber(daysInMonths, ref numberOfDays); ConsoleAppPrintUtil.PrintText($"Día: {numberOfDays} - Mes: { (MountNameEnum)monthNumber }"); } catch (Exception caught) { Console.WriteLine($"Excepción controlada: {caught}"); Console.Read(); } }
private static void Exercise3() { ConsoleAppPrintUtil.PrintTextWithLineSeparator("Ejercicio 3:"); try { var numberOfTheYear = ConsoleAppPrintUtil.PrintQuestionWithIntAnswerRecursive("Introduce un número de año: "); //if ((numberOfTheYear % 4 == 0) && (numberOfTheYear % 100 != 0 || numberOfTheYear % 400 == 0)) // ConsoleAppPrintUtil.PrintText("Es un año bisiesto"); //else // ConsoleAppPrintUtil.PrintText("NO es un año bisiesto"); var isLeapYear = (numberOfTheYear % 4 == 0) && (numberOfTheYear % 100 != 0 || numberOfTheYear % 400 == 0); var maxOfDay = isLeapYear ? 366 : 365; var numberOfDays = ConsoleAppPrintUtil.PrintQuestionWithIntPositiveOrNegativeAnswerRecursive($"Introduce un número de día entre 1 y {maxOfDay}: "); if (numberOfDays < 1 || numberOfDays > maxOfDay) { throw new ArgumentOutOfRangeException($"Día fuera de intervalo"); } var monthNumber = Exercise3MonthNumber(isLeapYear, ref numberOfDays); ConsoleAppPrintUtil.PrintText($"Día: {numberOfDays} - Mes: { (MountNameEnum)monthNumber }"); } catch (Exception caught) { Console.WriteLine($"Excepción controlada: {caught}"); Console.Read(); } }
private static void Exercise4() { ConsoleAppPrintUtil.PrintTextWithLineSeparator("Ejercicio 4:"); var questions = new string[] { "Introduzca el primer número: ", "Introduzca el segundo número: " }; var number1 = ConsoleAppPrintUtil.PrintQuestionWithIntPositiveOrNegativeAnswerRecursive(questions[0]); var number2 = ConsoleAppPrintUtil.PrintQuestionWithIntPositiveOrNegativeAnswerRecursive(questions[1]); try { var result = number1 / number2; ConsoleAppPrintUtil.PrintText($"El resultado de la división es: {result}"); } catch (DivideByZeroException) { ConsoleAppPrintUtil.PrintText("La división entre cero no está definida."); } catch (Exception) { ConsoleAppPrintUtil.PrintText("Ocurrió un error inesperado."); } }
private static void Exercise1() { ConsoleAppPrintUtil.PrintTextWithLineSeparator("Ejercicio 1:"); var questions = new string[] { "Introduzca el primer valor de la operación:", "Introduzca el segundo valor de la operación:" }; var number1 = ConsoleAppPrintUtil.PrintQuestionWithIntPositiveOrNegativeAnswerRecursive(questions[0]); var number2 = ConsoleAppPrintUtil.PrintQuestionWithIntPositiveOrNegativeAnswerRecursive(questions[1]); ConsoleAppPrintUtil.PrintDivision(number1, number2); }
private static void Exercise2() { ConsoleAppPrintUtil.PrintTextWithLineSeparator("Ejercicio 2:"); var questions = new string[] { "Introduce un número aleatorio: ", "Introduce otro número aleatorio: " }; var number1 = ConsoleAppPrintUtil.PrintQuestionWithIntPositiveOrNegativeAnswerRecursive(questions[0]); var number2 = ConsoleAppPrintUtil.PrintQuestionWithIntPositiveOrNegativeAnswerRecursive(questions[1]); ConsoleAppPrintUtil.PrintText(GetExercise2Text(number1, number2)); }
private static void Exercise14() { ConsoleAppPrintUtil.PrintTextWithLineSeparator("Ejercicio 14:"); var cantNumber = ConsoleAppPrintUtil.PrintQuestionWithIntAnswerRecursive("¿Cuántos números va a introducir?"); var numbers = new List <int>(); for (int i = 0; i < cantNumber; i++) { numbers.Add(ConsoleAppPrintUtil.PrintQuestionWithIntPositiveOrNegativeAnswerRecursive($"Introduzca el número {i + 1}:")); } var cantNegative = numbers.Count(number => number < 0); ConsoleAppPrintUtil.PrintText($"La cantidad de números negativos introducida es: {cantNegative}"); }
private static void Exercise8() { ConsoleAppPrintUtil.PrintTextWithLineSeparator("Ejercicio 8:"); var questions = new string[] { "Introduzca un número para calcular su factorial:" }; var number = ConsoleAppPrintUtil.PrintQuestionWithIntPositiveOrNegativeAnswerRecursive(questions[0]); try { decimal factory = GetFactoryWithCustomException(number); } catch (TePasasteException ex) { ConsoleAppPrintUtil.PrintText(ex.Message); } }
private static ArrayList Exercise9GetNumbers() { var numbers = new ArrayList(); var isNegativeNumber = false; do { var number = ConsoleAppPrintUtil.PrintQuestionWithIntPositiveOrNegativeAnswerRecursive("Introduzca un #:"); if (number >= 0) { numbers.Add(number); } else { isNegativeNumber = true; } } while (!isNegativeNumber); return(numbers); }
private static void Exercise15() { ConsoleAppPrintUtil.PrintTextWithLineSeparator("Ejercicio 15:"); var numbers = new List <int>(); var isNegative = false; do { var number = ConsoleAppPrintUtil.PrintQuestionWithIntPositiveOrNegativeAnswerRecursive($"Introduzca un número entero:"); if (number > 0) { numbers.Add(number); } else { isNegative = true; } } while (!isNegative && numbers.Count() < 10); ConsoleAppPrintUtil.PrintText($"Los números introducidos fueron: {String.Join(",", numbers)}"); }