Alexis-Coria

Untitled

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