Medals.java.html

  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              "Germany",
 16              "Korea",
 17              "Japan",
 18              "Russia",
 19              "United States"
 20           };
 21  
 22        int[][] counts =
 23           {
 24              { 1, 0, 1 },
 25              { 1, 1, 0 },
 26              { 0, 0, 1 },
 27              { 1, 0, 0 },
 28              { 0, 1, 1 },
 29              { 0, 1, 1 },
 30              { 1, 1, 0 }
 31           };
 32        
 33        System.out.println("        Country    Gold  Silver  Bronze   Total");
 34        
 35        // Print countries, counts, and row totals
 36        for (int i = 0; i < COUNTRIES; i++)
 37        {
 38           // Process the ith row
 39           System.out.printf("%15s", countries[i]);
 40  
 41           int total = 0; 
 42  
 43           // Print each row element and update the row total
 44           for (int j = 0; j < MEDALS; j++)
 45           {
 46              System.out.printf("%8d", counts[i][j]);
 47              total = total + counts[i][j];
 48           }
 49           
 50           // Display the row total and print a new line
 51           System.out.printf("%8d\n", total);
 52        }
 53     }
 54  }