How could I get the car with the highest number of kilometers asking the user to enter two identifiers of the cars?
public class Coches {
int identificador;
int kilometros;
public Coches(){}
public Coches(int identificador, int kilometros) {
this.identificador = identificador;
this.kilometros = kilometros;
}
public int getIdentificador(){
return identificador;
}
public int getKilometros(){
return kilometros;
}
}
public class Pruebas{
public static void main(String[] args) {
Scanner teclado = new Scanner(System.in);
Coches coche = new Coches();
// Se almacenan un numero máximo de coches (4).
Coches array [] = new Coches [4];
int contador = 0;
while (contador < array.length) {
System.out.println("Introduce el identificador del coche : ");
int identificador;
int kilometros;
System.out.print("Identificador :");
identificador = teclado.nextInt();
System.out.print("Kilometros :");
kilometros = teclado.nextInt();
coche = new Coches(identificador,kilometros);
array [contador] = coche;
contador++;
System.out.print("Coche dado de alta");
System.out.println();
}
System.out.print("Se ha alcanzado el maximo de coches.\n");
//Busca cual realizo mayor kilometraje:
int maxKilometros = 0;
int indiceCoche = 0;
for(int i = 0; i < array.length; i++){
if(array[i].getKilometros() > maxKilometros){
maxKilometros = array[i].getKilometros();
indiceCoche = i;
}
}
System.out.print("El coche que ha recorrido más kilometros es:" + array[indiceCoche].getIdentificador() + " Kilometros: "+ array[indiceCoche].getKilometros() +"\n");
}
}
I need in the last part to enter two identifiers by keyboard, and once introduced the compare and tell me which is the car with greater autonomy.
// Comparar dos coches introduciendo sus identificadores.
System.out.print("Introduzca el primer identificador :");
System.out.print("Introduzca el segundo identificador :");
How could it be done?
Thank you, Regards.
Thanks to Jorgesys for the answer in the previous thread.