I am making a basic election system, but I have a problem in telling which candidate was elected. In the case, I added three candidates (x, y, z) in ArrayList
, put two votes in the x, three in the y and four in the z. When I want to know what won (in this case the one that got the most votes) it prints all 3 candidates.
public void eleitoPresidente() {
int maior = 0;
for (Candidato e: candidato) {
if(e.votos > maior) {
maior = e.votos;
System.out.println("O presidente eleito é: " + e.nome);
}
}
}
I want it to print only the candidate with the most votes.