Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- /**
- *Numëron votat për kandidatët elektoralë.
- *input: një varg votash, i terminuar nga -1
- *output: lista e rezultateve të votave për kandidatë
- */
- public class VoteCount
- {
- public static void main(String[] args)
- {
- int numCandidates = 4;
- int[] votes = new int[numCandidates];
- boolean processing = true;
- while ( processing )
- { int v = new Integer(JOptionPane.showInputDialog
- ("Votoni për (0,1,2,3):")).intValue();
- if ( v == -1 )
- { processing = false; }
- else if ( v >= 0 && v < numCandidates )
- { votes[v]++; }
- else { JOptionPane.showMessageDialog(null,
- "Gabim në votim: " + v);
- }
- }
- // sortimi
- int[] indexat = { 0 , 1 , 2 , 3 };
- // pasi ne fillim kandidati identifikohet me ane te indeksave te count, pas sortimit gjithmone do fitonte kanditati 0, pasi numri me i madh do gjendej ne fillim
- // per kete arsye po e marrim nje varg te ri, ku tash kandidati indexat[i] ka numer te votave votes[i]
- boolean sorted = false;
- while( !sorted )
- {
- // bubble sort
- sorted = true; // po supozojme se eshte sortuar, nese e gjen nje rast qe duhet ndryshuar rradhitjen, e kthen prape ne false
- for( int i = 1; i!= numCandidates; i++ )
- {
- if( votes[i-1] < votes[i] )
- {
- int temp = votes[i-1];
- votes[i-1] = votes[i];
- votes[i] = temp;
- // rregullojme indeksat
- // kur nderrojme vendet e kandidatit psh 2 me 3 , atehere duhet edhe qe indeksat t'iau ndryshojme vendet,
- // ne menyre qe kandidati 2 (3) te dihet qe eshte ne pozite tjeter ne varg te votave
- temp = indexat[i-1];
- indexat[i-1] = indexat[i] ;
- indexat[i] = temp;
- sorted = false;
- }
- //nese s'ka pase ndryshime mrena loop-es for, sorted ka mbet true, pra dalim prej while !
- }
- }
- for ( int i = 0; i < numCandidates; i++ )
- {
- System.out.println("Kandidati " + indexat[i] + " ka "
- + votes[i] + " votë/a");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement