Advertisement
oona

Donations

Jan 22nd, 2014
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.00 KB | None | 0 0
  1.  
  2. public class donations {
  3.  
  4.     public static void main(String[] args) {
  5.         // TODO Auto-generated method stub
  6.        
  7.         int donations[] = {20, 15, 36, 31, 40, 45, 30, 100, 70, 50};
  8.         String names[] = {"Dina", "Charlie", "Charlize", "Roy", "Finn", "Olivia", "Amy", "James", "Annie", "Leo"};
  9.         int topDonation;
  10.         int temp1;
  11.         String temp2;
  12.        
  13.         for(int i=0; i<donations.length; i++)
  14.         {
  15.             topDonation = i;    //the highest amount of donations
  16.            
  17.             for(int j=i+1; j<donations.length; j++)
  18.             {
  19.                 if(donations[topDonation]<donations[j])
  20.                
  21.                     topDonation = j;
  22.                    
  23.             }//end of inner loop
  24.            
  25.             if(topDonation != i)
  26.             {
  27.                 temp1 = donations[i];
  28.                 donations[i] = donations[topDonation];
  29.                 donations[topDonation] = temp1;
  30.                
  31.                 temp2 = names[i];
  32.                 names[i] = names[topDonation];
  33.                 names[topDonation] = temp2;
  34.                
  35.             }
  36.            
  37.         }
  38.        
  39.         for(int i=0; i<donations.length; i++)
  40.             System.out.println(donations[i] + " DKK donated by " + names[i]);
  41.  
  42.     }//class ends
  43.  
  44. }//program ends
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement