In: Computer Science
Java
Starter Code:
import java.util.Scanner; Sample Output: Team Scores by quarter: Team 2 has won the game! |
Summary: The program will take inputs for the game on multiple quarters and display the final results along with winner. The total score is calulated by cobining the score in each quarter and the winner is found. The input validations are done before executing the program.
import java.util.Scanner;
public class GameScore {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
// Driver code
int team1[] = new int[4];
int team2[] = new int[4];
// Get the input from user
for (int qtr = 0; qtr < 4; qtr++) {
quarterScoring(team1, team2, qtr);
}
// Find the team total
int team1Total = teamTotal(team1);
int team2Total = teamTotal(team2);
// Display the result
displayGameResults(team1, team2);
if (team1Total > team2Total) {
System.out.println("Team 1 has won the game!");
} else {
System.out.println("Team 2 has won the game!");
}
}
// This method will return the total score of a team in all quarters
static int teamTotal(int[] team) {
int score = 0;
for (int qtr = 0; qtr < 4; qtr++) {
score += team[qtr];
}
return score;
}
// This method will read the team score details until user enters an team number /score
static void quarterScoring(int[] team1, int[] team2, int qtr) {
int team1Total = 0;
int team2Total = 0;
System.out.println("Quarter " + (qtr + 1));
while (true) {
System.out.println("Which team scored? (1 or 2)");
// Read the team number
int team = sc.nextInt();
// Validate the team number
if (team != 1 && team != 2) {
System.out.println("Invalid team - quarter has ended.");
break;
}
System.out.println("How many points did they score (1, 2, or 3)?");
int score = sc.nextInt();
// If the score in invalid then exit
if (score < 1 || score > 3) {
break;
} else if (team == 1) {
// Add the score to the total of team in the quarter
team1Total += score;
} else if (team == 2) {
team2Total += score;
}
team1[qtr] = team1Total;
team2[qtr] = team2Total;
}
}
// This method will print the output in a format
static void displayGameResults(int[] team1, int[] team2) {
int score = 0;
System.out.println("Team Scores by quarter:");
System.out.println("Q1 Q2 Q3 Q4 Total");
// Print output for each quarter for team 1
for (int qtr = 0; qtr < 4; qtr++) {
System.out.print(team1[qtr] + " ");
score+=team1[qtr];
}
System.out.println(score);
score = 0;
// Print output for each quarter for team 2
for (int qtr = 0; qtr < 4; qtr++) {
System.out.print(team2[qtr]+ " ");
score+=team2[qtr];
}
System.out.println(score);
}
}
Output
Quarter 1
Which team scored? (1 or 2)
1
How many points did they score (1, 2, or 3)?
1
Which team scored? (1 or 2)
2
How many points did they score (1, 2, or 3)?
2
Which team scored? (1 or 2)
3
Invalid team - quarter has ended.
Quarter 2
Which team scored? (1 or 2)
1
How many points did they score (1, 2, or 3)?
1
Which team scored? (1 or 2)
2
How many points did they score (1, 2, or 3)?
2
Which team scored? (1 or 2)
3
Invalid team - quarter has ended.
Quarter 3
Which team scored? (1 or 2)
1
How many points did they score (1, 2, or 3)?
1
Which team scored? (1 or 2)
2
How many points did they score (1, 2, or 3)?
2
Which team scored? (1 or 2)
3
Invalid team - quarter has ended.
Quarter 4
Which team scored? (1 or 2)
1
How many points did they score (1, 2, or 3)?
1
Which team scored? (1 or 2)
2
How many points did they score (1, 2, or 3)?
2
Which team scored? (1 or 2)
3
Invalid team - quarter has ended.
Team Scores by quarter:
Q1 Q2 Q3 Q4 Total
1 1 1 1 4
2 2 2 2 8
Team 2 has won the game!