Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void ejercicio1(){
- //1. Leer todos los números enteros hasta que el usuario ingrese
- // el comando SALIR e imprimir el promedio, el máximo y el mímino.
- int numero = 0;
- Scanner scanner = new Scanner(System.in);
- int acumulador = 0;
- int maximo = 0;
- int minimo = 0;
- int contador = 1;
- while(true)
- {
- System.out.println("Ingrese un número " + contador + ":");
- if (scanner.hasNextInt())
- {
- numero = scanner.nextInt();
- acumulador = acumulador + numero;
- if (contador == 1)
- {
- maximo = numero;
- minimo = numero;
- }else{
- if (numero > maximo)
- maximo = numero;
- if (numero < minimo)
- minimo = numero;
- }
- contador++;
- }else
- {
- System.out.println("Número incorrecto, ingrese nuevamente:");
- String comando = scanner.next();
- if (comando.toUpperCase().equals("SALIR"))
- break;
- }
- }
- System.out.println("El promedio es: " + acumulador/5);
- System.out.println("El máximo es: " + maximo);
- System.out.println("El mínimo es: " + minimo);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement