Alexis-Coria

Punto 2

Aug 30th, 2021
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 KB | None | 0 0
  1. import java.util.Random;
  2. import java.util.Scanner;
  3.  
  4. public class Resolucion {
  5.  
  6. public static void main(String[] args) {
  7. int opc = 0,a , b , result , sumtotal = 0, i=0 ,prodtotal=0, errores=0;
  8. Scanner sn = new Scanner(System.in);
  9. while (opc != 4) {
  10. menus();
  11. opc=validar(sn,"Seleccione una opcion.");
  12. switch (opc)
  13. {
  14. case 1:
  15. a=validar(sn,"Ingrese valor de a: ");
  16. b=validar(sn,"Ingrese valor de b: ");
  17. result=opdatos(a,b);
  18. if (result == a+b)
  19. { sumtotal = sumtotal + result;
  20. }
  21. if (result == a*b)
  22. { prodtotal = prodtotal + result;
  23. i=i+1;
  24. }
  25. if (result == 0)
  26. { errores = errores + 1;
  27. break;
  28. }
  29. System.out.println("El resultado es: "+ result);
  30. break;
  31.  
  32. case 2:
  33. System.out.println("Generando valores random para a y b.");
  34. Random ar= new Random();
  35. int ar1 = ar.nextInt();
  36. Random br = new Random();
  37. int br1 = br.nextInt();
  38. result= opdatos(ar1,br1);
  39.  
  40. if (result == ar1+br1)
  41. { sumtotal = sumtotal + result;
  42. }
  43. if (result == ar1*br1)
  44. { prodtotal = prodtotal + result;
  45. i=i+1;
  46. }
  47. if (result == 0)
  48. { errores = errores + 1;
  49. break;
  50. }
  51. System.out.println("El resultado es: "+ result);
  52. break;
  53. case 3:
  54. System.out.println("El total de sumas acumuladas es: "+ sumtotal);
  55. System.out.println("El total de errores es: "+ errores);
  56. if (prodtotal !=0)
  57. {System.out.println("El promedio de productos es: "+ prodtotal/i);
  58. }
  59. break;
  60. case 4:
  61. System.out.println("Cerrando programa...");
  62. break;
  63. default:
  64. System.out.println("Seleccione una opcion valida.");
  65. }
  66.  
  67. }
  68. }
  69.  
  70.  
  71. public static String entradalog() {
  72. Scanner sn = new Scanner(System.in);
  73. String teclado = sn.next();
  74. return teclado;
  75. }
  76.  
  77. public static void menus() {
  78. int sel;
  79. System.out.println("Calculadora");
  80. System.out.println("Seleccione una opcion.");
  81. System.out.println("1 - Ingreso de datos.");
  82. System.out.println("2 - Numeros al azar.");
  83. System.out.println("3 - Total de procedimientos.");
  84. System.out.println("4 - Finalizar");
  85.  
  86. }
  87.  
  88.  
  89. public static int opdatos(int x , int y){
  90. String s;
  91. int op = 0;
  92. System.out.println("Ingrese operador logico.");
  93. s=entradalog();
  94. switch (s)
  95. {
  96. case "+":
  97. op = x+y;
  98. break;
  99. case "-":
  100. op = x-y;
  101. break;
  102. case "*":
  103. op = x*y;
  104. break;
  105. case "/":
  106. if (y!=0)
  107. { op = x/y;
  108. }
  109. System.out.println("Error: Division por 0.");
  110. return 0;
  111. default:
  112. System.out.println("Ingrese un operador logico valido ( + , - , * , / ).");
  113. }
  114.  
  115. return op;
  116. }
  117.  
  118. public static int validar(Scanner b, String ingreso) {
  119. int numero;
  120. String linea;
  121. while (true) {
  122. try {
  123. System.out.println(ingreso);
  124. linea=b.nextLine();
  125. numero=Integer.parseInt(linea);
  126. break;
  127. }
  128. catch(Exception e){
  129. System.out.println("Error: Ingrese un numero.");
  130. }
  131. }
  132. return numero;
  133. }
  134.  
  135. }
Advertisement
Add Comment
Please, Sign In to add comment