Advertisement
Guest User

Untitled

a guest
Feb 13th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. public static void ejercicio1(){
  2. //1. Leer todos los números enteros hasta que el usuario ingrese
  3. // el comando SALIR e imprimir el promedio, el máximo y el mímino.
  4.  
  5. int numero = 0;
  6. Scanner scanner = new Scanner(System.in);
  7. int acumulador = 0;
  8. int maximo = 0;
  9. int minimo = 0;
  10. int contador = 1;
  11.  
  12. while(true)
  13. {
  14. System.out.println("Ingrese un número " + contador + ":");
  15.  
  16. if (scanner.hasNextInt())
  17. {
  18. numero = scanner.nextInt();
  19. acumulador = acumulador + numero;
  20.  
  21. if (contador == 1)
  22. {
  23. maximo = numero;
  24. minimo = numero;
  25. }else{
  26. if (numero > maximo)
  27. maximo = numero;
  28. if (numero < minimo)
  29. minimo = numero;
  30. }
  31. contador++;
  32. }else
  33. {
  34. System.out.println("Número incorrecto, ingrese nuevamente:");
  35. String comando = scanner.next();
  36. if (comando.toUpperCase().equals("SALIR"))
  37. break;
  38. }
  39. }
  40.  
  41. System.out.println("El promedio es: " + acumulador/5);
  42. System.out.println("El máximo es: " + maximo);
  43. System.out.println("El mínimo es: " + minimo);
  44.  
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement