1  /**
  2     This program prints a table of medal winner counts with row totals.
  3  */
  4  public class Medals
  5  {
  6     public static void main(String[] args)
  7     {
  8        final int COUNTRIES = 7;
  9        final int MEDALS = 3;
 10  
 11        String[] countries = 
 12           { 
 13              "Canada",
 14              "China",
 15              "Japan",
 16              "Russia",
 17              "Switzerland",
 18              "Ukraine",
 19              "United States" 
 20           };
 21        
 22        int[][] counts = 
 23           { 
 24              { 0, 0, 1 },
 25              { 0, 1, 1 }, 
 26              { 1, 0, 0 }, 
 27              { 3, 0, 1 }, 
 28              { 0, 1, 0 }, 
 29              { 0, 0, 1 },
 30              { 0, 2, 0 }
 31           }; 
 32        
 33        System.out.println("        Country    Gold  Silver  Bronze   Total");
 34        
 35        // Print countries, counts, and row totals
 36  
 37        for (int i = 0; i < COUNTRIES; i++)
 38        {
 39           // Process the ith row
 40  
 41           System.out.printf("%15s", countries[i]);
 42  
 43           int total = 0; 
 44  
 45           // Print each row element and update the row total
 46  
 47           for (int j = 0; j < MEDALS; j++)
 48           {
 49              System.out.printf("%8d", counts[i][j]);
 50              total = total + counts[i][j];
 51           }
 52           
 53           // Display the row total and print a new line
 54  
 55           System.out.printf("%8d\n", total);
 56        }
 57     }
 58  }