3
I’m doing a basic election system, but I have a problem telling which candidate was elected. In this case, I added three candidates (x, y, z) to a ArrayList
, I put two votes on x, three on y and four on z. When I want to know which one won (in this case which got the most votes) he 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 him to print only the candidate with the largest number of votes.
removes the println from the for and displays the Candidate outside of it.
– user28595