/*******************************************************************************/
/** A question about cricket players. */
/** Sorting players' names alphabetically. */
/** Conclusion of the number of players who played more than 200 games. */
/*******************************************************************************/
package com.company;
import java.util.Scanner;
public class Main {
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
int num = 0; //number of players
System.out.println("Please enter the number of players.");
while (num <= 0 )
{
System.out.println("Enter an integer greater than zero. ");
if(scan.hasNextInt()) //enter an integer greater than zero.
{
num = scan.nextInt();
}
else
{
scan.nextLine();
}
}
scan.nextLine();
String[] cricketers = new String[num];
int[] numGame = new int[num];
for(int i=0; i<num; i++)
{
System.out.println("Enter player name with number " + String.valueOf(i+1) + " : ");
cricketers[i] = scan.nextLine();
numGame[i] = -1;
System.out.println("Enter the number of games he played. ");
while (numGame[i] < 0 ) //The number of games cannot be a negative number.
{
if(scan.hasNextInt())
{
numGame[i] = scan.nextInt();
}
else
{
scan.nextLine();
}
}
scan.nextLine();
}
String[] s = sort(cricketers);
for(int i=0; i<num; i++)
{
System.out.println(s[i]);
}
System.out.println("\n" + more200(numGame) + " players have played more than 200 matches.");
scan.close();
}
/**selection sort*/
public static String[] sort(String[] str)
{
int i, j;
int len = str.length;
String s;
for(i=0; i<(len-1); i++)
{
for(j=i+1; j<(len); j++)
{
if(str[i].compareTo(str[j]) > 0)
{
s = str[i];
str[i] = str[j];
str[j] = s;
}
}
}
return str;
}
/**returns the number of players who played more than 200 games*/
public static int more200(int[] game)
{
int match = 0;
for(int i=0; i<game.length; i++)
{
if(game[i]>200)
{
match++;
}
}
return match;
}
}
Comments
Leave a comment