Advertisement
Kushtrim

VoteCount

Apr 30th, 2013
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.41 KB | None | 0 0
  1. import javax.swing.*;
  2. /**
  3.  *Numëron votat për kandidatët elektoralë.
  4.  *input: një varg votash, i terminuar nga -1
  5.  *output: lista e rezultateve të votave për kandidatë
  6.  */
  7. public class VoteCount
  8. {
  9.     public static void main(String[] args)
  10.     {
  11.         int numCandidates = 4;
  12.         int[] votes = new int[numCandidates];
  13.         boolean processing = true;
  14.         while ( processing )
  15.         { int v = new Integer(JOptionPane.showInputDialog
  16.                     ("Votoni për (0,1,2,3):")).intValue();
  17.             if ( v == -1 )
  18.             { processing = false; }
  19.             else if ( v >= 0 && v < numCandidates )
  20.             { votes[v]++; }
  21.             else { JOptionPane.showMessageDialog(null,
  22.                     "Gabim në votim: " + v);
  23.             }
  24.         }
  25.  
  26.         // sortimi
  27.         int[] indexat = { 0 , 1 , 2 , 3 };
  28.         //  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
  29.         //  per kete arsye po e marrim nje varg te ri, ku tash kandidati indexat[i] ka numer te votave votes[i]
  30.        
  31.         boolean sorted = false;
  32.         while( !sorted )
  33.         {
  34.             // bubble sort
  35.             sorted = true;  // po supozojme se eshte sortuar, nese e gjen nje rast qe duhet ndryshuar rradhitjen, e kthen prape ne false
  36.             for( int i = 1; i!= numCandidates; i++ )
  37.             {
  38.                 if( votes[i-1] < votes[i] )
  39.                 {
  40.                     int temp = votes[i-1];
  41.                     votes[i-1] = votes[i];
  42.                     votes[i] = temp;
  43.  
  44.                     // rregullojme indeksat
  45.                     // kur nderrojme vendet e kandidatit psh 2 me 3 , atehere duhet edhe qe indeksat t'iau ndryshojme vendet,
  46.                     // ne menyre qe kandidati 2 (3) te dihet qe eshte ne pozite tjeter ne varg te votave
  47.                     temp = indexat[i-1];
  48.                     indexat[i-1] = indexat[i] ;
  49.                     indexat[i] = temp;
  50.  
  51.                     sorted = false;
  52.                 }
  53.    
  54.                 //nese s'ka pase ndryshime mrena loop-es for, sorted ka mbet true, pra dalim prej while !
  55.             }
  56.  
  57.         }
  58.  
  59.         for ( int i = 0; i < numCandidates; i++ )
  60.         {
  61.             System.out.println("Kandidati " + indexat[i] + " ka "
  62.                 + votes[i] + " votë/a");
  63.         }
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement