View difference between Paste ID: 2ihsqmtJ and LszN9xpY
SHOW: | | - or go back to the newest paste.
 1 ```// ``` 2 ```// Created by Julio Tentor ``` 3 ```// ``` 4 5 ```/* ``` 6 7 ```Encolar números en una cola hasta que se ingrese el número 99; ``` 8 ```luego desencolarlos y hacer lo siguiente: calcular el factorial ``` 9 ```de los números positivos, sumar los negativos y contar los ceros. ``` 10 11 ``` */ ``` 12 13 ```import java.util.Random; ``` 14 15 ```public class CasoEjemplo_a { ``` 16 17 ``` private static Random random = new Random(); ``` 18 19 ``` public void Run() { ``` 20 21 ``` System.out.println("Trabajo Práctico Nº 3 - Caso Ejemplo a)\n"); ``` 22 23 ``` Queue numbers1 = new Queue(10); ``` 24 ``` Queue numbers2 = new Queue(10); ``` 25 26 ``` int number = 0; ``` 27 28 ``` StringBuilder sb = new StringBuilder(); ``` 29 ``` for (int count = random.nextInt(20) + 1; count > 0; --count) { ``` 30 31 ``` number = random.nextInt(50) - 25; ``` 32 ``` if (number == 99) { ``` 33 ``` break; ``` 34 ``` } ``` 35 ``` sb.append(number +" "); ``` 36 37 ``` try { ``` 38 ``` numbers1.add(number); ``` 39 ``` } catch (Exception exception) { ``` 40 ``` System.out.println("Excepción atrapada para numbers1 " + exception.getMessage() ); ``` 41 ``` } ``` 42 43 ``` try { ``` 44 ``` numbers2.offer(number); ``` 45 ``` } catch (Exception exception) { ``` 46 ``` System.out.println("Excepción atrapada para numbers2 " + exception.getMessage() ); ``` 47 ``` } ``` 48 ``` } ``` 49 ``` System.out.println("\nGenerados: " + sb.toString()); ``` 50 51 ``` System.out.println("Cola numbers1 (" + numbers1.size() +") " + numbers1.toString()); ``` 52 ``` System.out.println("Cola numbers2 (" + numbers2.size() +") " + numbers2.toString()); ``` 53 54 ``` int negativeSum = 0; ``` 55 ``` int zeroCount = 0; ``` 56 57 ``` while (!numbers1.isEmpty()) { ``` 58 ``` number = numbers1.remove(); ``` 59 ``` System.out.print("\nNúmero: " + number); ``` 60 ``` if (number == 0) { ``` 61 ``` ++zeroCount; ``` 62 ``` continue; ``` 63 ``` } ``` 64 ``` if (number > 0) { ``` 65 ``` System.out.print(" Factorial: " + Factorial(number)); ``` 66 ``` } else { ``` 67 ``` negativeSum += number; ``` 68 ``` } ``` 69 ``` } ``` 70 ``` System.out.println("\n\nSuma de los negativos: " + negativeSum); ``` 71 ``` System.out.println("Cuenta de los ceros..: " + zeroCount); ``` 72 73 ``` } ``` 74 75 ``` private long Factorial(int number) { ``` 76 ``` long result = 1; ``` 77 ``` for( ; number > 1; --number ) { ``` 78 ``` result *= number; ``` 79 ``` } ``` 80 ``` return result; ``` 81 ``` } ``` 82 ```} ``` 83 84 85