Java program for Cricket Team

Java program

Java program for Cricket Team

Question : Write a program to print 11 players of a Cricket team with their names, runs and strike rate. Print also the highest run getter in the team with his name.
import java.io.*;
public class CricketTeam
{
    public static void main(String args[]) throws IOException
    {
        // Variables
        int run [] = new int [11];
        int ball [] = new int [11];
        String name [] = new String [11];
        int i, j , t1, t2, p=0;
        int total = 0;      //  Total number
        int max;            //  for maximum
        String t3;           // Temporary variable
        double strike;      // Strike rate calculation
        InputStreamReader in = new InputStreamReader (System.in);
        BufferedReader br = new BufferedReader (in);
        // Input the elements in the arrays
        System.out.println("Enter Player name, runs and Ball played:");
        for(i=0; i<11; i++)
        {
            name[i] = br.readLine();
            run[i] = Integer.parseInt(br.readLine());
            ball[i] = Integer.parseInt(br.readLine());
        }
        // Sorting the player name with thier runs and balls played
        for(i=0; i<10 ;i++)
        {
            for(j=0; j<10-i; j++)
            {
                if(name[j].compareTo(name[j+1])>0)
                {
                    t3 = name[j];
                    name[j] = name[j+1];
                    name[j+1] = t3;
                    t1 = run[j];
                    run[j] = run[j+1];
                    run[j+1] = t1;
                    t2 = ball[j];
                    ball[j] = ball[j+1];
                    ball[j+1] = t2;
                }
            }
        }



        // Printing the value and finding the maximum score
        max=run[0];    // Storing run at 0 position

        System.out.println("\n\n Position\t Name\t Run\t Ball\t Strike Rate");
        System.out.println("========\t ============\t==========\t===========");
        for(i=0; i<11; i++)
        {
            strike = (double)100*run[i]/ball[i];
            total = total + run[i];
            // checking for highest run getter with the position
            if(run[i]>max)
            {
                max = run[i];
                p=i;
            }
            System.out.println(i+1 + "\t\t" + name[i] + "\t" + run[i] + "\t" + ball[i] + "\t" + strike);
        }
        System.out.println("=================================================");
        System.out.println("\t\t\t\t" + total);
        System.out.println("Highest run =  " + run[p] + " by " + name[p]);
    }
}

Sample output:
Kholi
80
50
Vijay
50
60
Rahul
70
90
Sachin
100
100
Ajinka
75
70
Yusuf
50
30
Ashwin
20
20
Shami
10
10
Umesh
0
0
Patel
20
10
Aron
0
0



Position                Name   Run       Ball        Strike Rate
========           ====== ======              =====   ==========
1                              Ajinka   75           70           107.14285714285714
2                              Aron      0              0              NaN
3                              Ashwin 20           20           100.0
4                              Kholi      80           50           160.0
5                              Patel      20           10           200.0
6                              Rahul     70           90           77.77777777777777
7                              Sachin   100         100         100.0
8                              Shami    10           10           100.0
9                              Umesh 0              0              NaN
10                           Vijay      50           60           83.33333333333333
11                           Yusuf     50           30           166.66666666666666
=================================================
                                                                               475
Highest run =  100 by Sachin


SHARE THIS
Previous Post
Next Post