Menu
Topics Index
...
`

Store team scores for 3 years - Java Example Program


Store team scores for 3 years
class Store3YearsTeamScores
{
    public static void main(String arg[])
    {
        // India - team 0
        // Pakistan - team 1
        // Australia - team 2
        // Srilanka - team 3
        // 2008 - year 0
        // 2009 - year 1
        // 2010 - year 2
        
        
        int[][] scores = new int[3][4];
        
        scores[0][0] = 150; // Score in year 2008 (year 0) for India (team 0)
        scores[0][1] = 170; // Score in year 2008 (year 0) for Pakistan (team 1)
        scores[0][2] = 183; // Score in year 2008 (year 0) for Australia (team 2)
        scores[0][3] = 216; // Score in year 2008 (year 0) for Srilanka (team 3)
        
        scores[1][0] = 258; // Score in year 2009 (year 1) for India (team 0)
        scores[1][1] = 221; // Score in year 2009 (year 1) for Pakistan (team 1)
        scores[1][2] = 241; // Score in year 2009 (year 1) for Australia (team 2)
        scores[1][3] = 230; // Score in year 2009 (year 1) for Srilanka (team 3)
        
        scores[2][0] = 279; // Score in year 2010 (year 2) for India (team 0)
        scores[2][1] = 260; // Score in year 2010 (year 2) for Pakistan (team 1)
        scores[2][2] = 250; // Score in year 2010 (year 2) for Australia (team 2)
        scores[2][3] = 274; // Score in year 2010 (year 2) for Srilanka (team 3)
        
        System.out.println("Srilanka scored " + scores[1][3] + " in the year 2009.");
        System.out.println("India scored " + scores[2][0] + " in the year 2010.");
        int total_australia_score = scores[0][2] + scores[1][2] + scores[2][2];
        System.out.println("Australia scored " + total_australia_score + " for years 2008, 2009 and 2010.");
        int all_teams_2009 = scores[1][0] + scores[1][1] + scores[1][2] + scores[1][3];
        System.out.println("All teams scored " + all_teams_2009 + " in the year 2009.");
    
    }
}
OUTPUT

Srilanka scored 230 in the year 2009.
India scored 279 in the year 2010.
Australia scored 674 for years 2008, 2009 and 2010.
All teams scored 950 in the year 2009.

DESCRIPTION

Here we have declared a two dimensional array to store the team scores of various cricket teams in years 2008, 2009 and 2010. Every team is given a unique number (used as index) and every year is also given a unique number. The scores are initialized using indices and some of scores are printed, they are also accessed using indices.

THINGS TO TRY
  • Change Pakistan's score in year 2010 from 260 to 255 and add a print statement to print Pakistan's score.
  • Write code to print the total score achieved by all teams in the year 2010.
  • Write code to print the total score of achieved by India in all years.

© meritcampus 2019

All Rights Reserved.

Open In App