In: Computer Science
Abdullah is a batsman who has played 10 matches in a local cricket club. Take user input in a 2D array to store his score in first row and second row for balls he has faced in each match.
Calculate and display the strike rate (SR) of Mr. Abdullah in all 10 matches he has played.
SR=score/number of balls faced×100
Count and display the number of 50s and 100s he has score.
import java.util.*;
import java.io.*;
class Main
{
public static void main(String args[]) throws Exception
{
//variable declaration
int number_of_matches=10;
int score=0,balls=0;
int fifty_count=0,century_count=0;
Scanner sc = new Scanner(System.in);//object of Scanner
int[][] matches = new int[2][number_of_matches];//array initialization
System.out.println("Enter scores and number of ball face by Abdullah ");
//for loop to take user input for scores and balls faced
for(int i=0;i<2;i++)
{
if((i+1)==1){
System.out.println("Enter scores in first row");
for(int j=0;j<number_of_matches;j++)
{
matches[i][j] = sc.nextInt();
}
}
else if((i+1)==2)
{
System.out.println("Enter number balls faced in second row");
for(int j=0;j<number_of_matches;j++)
{
matches[i][j] = sc.nextInt();
}
}
}
//for loop to print the scores and balls faced
for(int i=0;i<2;++i)
{
if((i+1)==1)
{
System.out.println("Scores:");
for(int j=0;j<number_of_matches;j++)
{
System.out.print(matches[i][j]+"\t");
}
}
else if((i+1)==2)
{
System.out.println("\nNumber balls faced:");
for(int j=0;j<number_of_matches;j++)
{
System.out.print(matches[i][j]+"\t");
}
}
}
//for loop to calculate Total score and total number of balls faced
for(int i=0;i<2;i++)
{
if((i+1)==1)
{
for(int j=0;j<number_of_matches;j++)
{
score += matches[i][j]; //total score
}
}
else if((i+1)==2)
{
for(int j=0;j<number_of_matches;j++)
{
balls += matches[i][j]; //total balls faced
}
}
}
//prints total score and total number of balls faced
System.out.println("\nTotal Score:"+score);
System.out.println("Total Balls faced:"+balls);
//calculate the strike rate
double Strike_rate = (score/balls)*100;
//for loop to count number of centuries and number of half centuries
for(int i=0;i<1;i++)
{
for(int j=0;j<number_of_matches;j++){
if(matches[i][j]>=50 && matches[i][j]<100)
{
fifty_count++;
}
else if(matches[i][j]>=100 && matches[i][j]<200)
{
century_count++;
}
}
}
//output Strike_rate,fifty_count and century_count
System.out.println("Strike rate of Abdullah: "+Strike_rate);
System.out.println("Number of half century scored by Abdullah is: "+fifty_count);
System.out.println("Number of centuries scored by Abdullah is: "+century_count);
}}
Output::
If any queries please comment and do like