142k views
3 votes
This program will keep track of win-tied-loss and points earned records for team. There are 6 teams and each week there are three games (one game per team per week). Enter the team numbers and game scores in an array within the program rather than user typing at command prompt.After reading in each week, the program should print out the win-tied-loss records and points earned for each team. A win is two points, tied game is one point and a loss is zero points. For example:How many weeks of data: 3For week 1, game 1, enter the two teams and the score: 0 1 1 4That is in week 1, game 1 is between team 0 team 1. Final scores are team 0 is 1 and team 1 is 4. Therefore, team 1 has 2 points, team 0 has 0 points. Similarly,For week 1, game 2, enter the two teams and the score: 2 3 1 2For week 1, game 3, enter the two teams and the score: 4 5 2 0For week 2, game 1, enter the two teams and the score: 0 2 3 0For week 2, game 2, enter the two teams and the score: 1 4 0 1For week 2, game 3, enter the two teams and the score: 3 5 4 4For week 3, game 1, enter the two teams and the score: 0 4 8 7For week 3, game 2, enter the two teams and the score: 1 5 0 0For week 3, game 3, enter the two teams and the score: 2 3 6 9

User Gyebro
by
5.0k points

1 Answer

3 votes

Answer:

Check the explanation

Step-by-step explanation:

import java.util.*;

public class TeamRecords {

public static void main(String[] args) {

int teams = 6;

System.out.print("How many weeks of data: ");

Scanner sc = new Scanner(System.in);

System.out.println();

int weeks = sc.nextInt();

int[] wins = new int[teams];

int[] ties = new int[teams];

int[] losses = new int[teams];

//Each entry in points is an array with two elements

//the first element is the team and the second element is the points

//This will keep the team associated with the points when we sort the array

int[][] points = new int[teams][2];

for (int i = 0; i<teams; i++) {

points[i][0] = i;

}

int[] pointsFor = new int[teams];

int[] pointsAgainst = new int[teams];

for (int week=1; week <= weeks; week++) {

System.out.println();

for (int game=1; game <= teams/2; game++) {

System.out.print("For week "+week+", game "+game+", enter the two teams and the score: ");

int team1 = sc.nextInt();

int team2 = sc.nextInt();

int score1 = sc.nextInt();

int score2 = sc.nextInt();

if (score1 > score2) {

wins[team1]++;

losses[team2]++;

points[team1][1] += 2;

} else if (score1 < score2) {

wins[team2]++;

losses[team1]++;

points[team2][1] += 2;

} else {

ties[team2]++;

ties[team1]++;

points[team1][1] ++;

points[team2][1] ++;

}

pointsFor[team1] += score1;

pointsFor[team2] += score2;

pointsAgainst[team1] += score2;

pointsAgainst[team2] += score1;

}

}

System.out.println();

System.out.println("League Standing after 2 weeks:");

System.out.println();

System.out.println("W T L");

for (int team=0; team < teams; team++) {

System.out.println("Team "+team+" "+wins[team]+" "+ties[team]+" "+losses[team]);

}

System.out.println();

System.out.println("Points Table:");

// sort the points array in descending order

// based on the number of points earned by each team

// (which is the second element of each int array that makes up the points array)

Arrays.sort(points,new Comparator<int[]>() {

public int compare(int[] o1, int[] o2) {

return (new Integer(o2[1])).compareTo(o1[1]);

}

});

System.out.println();

for (int i=0; i<points.length; i++) {

System.out.println("Team "+points[i][0]+" "+points[i][1]);

}

System.out.println();

System.out.println("Winning percentages: ");

for (int i=0; i<teams; i++) {

System.out.println("Team "+i+" "+(wins[i]*100/(new Float(weeks)))+"%");

}

System.out.println();

System.out.println("Points scored for/against:");

for (int i=0; i<teams; i++) {

System.out.println("Team "+i+" "+pointsFor[i]+"/"+pointsAgainst[i]);

}

}

}

User Nello
by
5.1k points