-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTotalPoints.java
More file actions
42 lines (38 loc) · 1.6 KB
/
TotalPoints.java
File metadata and controls
42 lines (38 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//8th kyu
//Our football team has finished the championship.
//Our team's match results are recorded in a collection of strings.
//Each match is represented by a string in the format "x:y", where x is our team's score and y is our opponents score.
//Ex: For example: ["3:1", "2:2", "0:1", ...]
//Rule 1: if x > y: 3 points (win)
//Rule 2: if x < y: 0 points (loss)
//Rule 3: if x = y: 1 point (tie)
//write method that takes this collection and returns the number of points our team (x) got in the championship by the rules given above.
public class TotalPoints {
public static int points(String[] games) {
//implement me
int totalP = 0; // Initialize total points accumulator
for (String game : games) {
String[] scores = game.split(":"); // Split the game string into home and away scores
int x = Integer.parseInt(scores[0]); // Convert home score to integer
int y = Integer.parseInt(scores[1]); // Convert away score to integer
// Validate score ranges
if (x < 0 || x > 4 || y < 0 || y > 4) {
throw new IllegalArgumentException("Scores must be between 0 and 4");
}
// Calculate points based on the game result
if (x > y) {
totalP += 3; // Win
}
else if (x < y) {
totalP += 0; // Loss (implicitly, could be omitted)
}
else {
totalP += 1; // Tie
}
}
return totalP;
}
public static void main(String[] args){
System.out.println(points(new String[]{"3:1", "2:2", "0:1"}));
}
}