Necesito el algoritmo en java para calcular en 5 números cual es el mayor y cual es el menor

este es mi comienzo...

import java.io.*;
class Mayores{
public static void main(String args[])throws java.io.IOException{
int may=0, men=999999999, i=1, n=0;
BufferedReader mar=new BufferedReader (new InputStreamReader(System.in));
for(i=1;i<=5;i++){
System.out.println("Ingresa un numero");
System.out.flush();
n=Integer.parseInt(mar.readLine());
if(n>may){
may=n;
}
if(n<men){
men=n;
}
}
System.out.println("El numero mayor es: "+may);
System.out.println("El numero menor es: "+men);
}
}

1 respuesta

Respuesta

Solo debes de usar una bandera se hace así

int num_menor ;

empiezas siempre el num_menor en el primer numero por tanto es algo así

int cant_leidas = 0;

if ( cant_leidas == 0 ) num_menor = tuimput;

else {

if (tuimput < num_menor) num_menor = tuinput;

}

cant_leidas++;

Eso debería de tener tu condicional, cualquier cosa me preguntas o me das mas información sobre tu input y te lo desarrollo :) !

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas