samedi 10 mars 2018

Decrease the use of 'ifs' in Java Application

I am studying Java and my teacher has passed an exercise to the class where there are 4 candidates for an election, and the problem is to check which candidate is the winner and whether or not there will be a second round (show the candidates present and the corresponding votes ). The problem is that it can not be done using arrays, which means I have to do it using only 'if's'. How do you do it the quickest, keeping in mind that it is possible for both 2 and the 4 candidates to go to the second round?

My progress so far:

if(voteLula > voteCiro && voteLula > voteCriancinha && voteLula > voteRose){
       winnervotes = votosLula;
       winner = "Lulalelé";
   }else if(voteCiro > voteLula && voteCiro > voteCriancinha && voteCiro > voteRose){
       winnervotes = votosCiro;
       winner = "Cirogrude";
   }else if(voteCriancinha > voteLula && voteCriancinha > voteCiro && voteCriancinha > voteRose){
       winnervotes = votosCriancinha;
       winner = "Criancinha";
   }else if(voteRose > voteLula && voteRose > voteCiro && voteRose > voteCriancinha){
       winnervotes= votosRose;
       winner = "Roseaçaí";
   }

I thought about using "> =" within those loops, but would greatly increase the lines of code.

Thank you in advance.

Aucun commentaire:

Enregistrer un commentaire