Click here to Skip to main content
15,889,909 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So Basically i wrote this code for an assignment.
It is a scoring system.
I am pretty new to programming as well.

You can see that my if- else code is long and i want to shorten it.
If there are 5 teams then i am going to have to write so many if-else codes just for condition checking.
How can i shorten it and make it go as far as it can?
Java
            if (teamNo == 2) {
                if (teamScore[0] > teamScore[1]) {
                    System.out.println(teamName[0] + " is the winner of the event");
                }
                if (teamScore[1] > teamScore[0]) {
                    System.out.println(teamName[1] + " is the winner of the event");
                }
                if (teamNo == 3) {
                    if (teamScore[0] > teamScore[1] && teamScore[0] > teamScore[2]) {
                        System.out.println(teamName[0] + " is the winner of the event");
                    }
                    if (teamScore[1] > teamScore[0] && teamScore[1] > teamScore[2]) {
                        System.out.println(teamName[1] + " is the winner of the event");
                    }
                    if (teamScore[2] > teamScore[0] && teamScore[2] > teamScore[1]) {
                        System.out.println(teamName[2] + " is the winner of the event");
                    }

                }
                if (teamNo == 4) {
                    if (teamScore[0] > teamScore[1] && teamScore[0] > teamScore[2] && teamScore[0] > teamScore[3]) {
                        System.out.println(teamName[0] + " is the winner of the event");
                    }
                    if (teamScore[1] > teamScore[0] && teamScore[1] > teamScore[2] && teamScore[1] > teamScore[3]) {
                        System.out.println(teamName[1] + " is the winner of the event");
                    }
                    if (teamScore[2] > teamScore[0] && teamScore[2] > teamScore[1] && teamScore[2] > teamScore[3]) {
                        System.out.println(teamName[2] + " is the winner of the event");
                    }
                    if (teamScore[3] > teamScore[0] && teamScore[3] > teamScore[1] && teamScore[3] > teamScore[2]) {
                        System.out.println(teamName[3] + " is the winner of the event");
                    }
                }
            }
        }
    }
}


What I have tried:

This is my best attempt . I know that there is a way to shorten it
I just dont know how.
Posted
Updated 20-Aug-21 22:34pm
v3

The way I'd do it is different: I'd create a Team class, which contained a team number, a team name, and a team score - then create a collection (such as an array) of Team objects ordered by team number - so the team number is the same as the collection index.

Then it becomes trivial: use the built in functions to sort your collection by team score and it's two lines of code to find the winner!
Plus, you can print score sheets with a simple loop, ordered by team number, name, or score as necessary, and it works if the number of teams grows to five, or ten, or one hundred!

Have a think about it, and see what changes you'd need to make to the rest of your code - it would probably simplify that enormously as well, along with adding a lot of flexibility!
 
Share this answer
 
All you need to do is to find the team with the highest score. You can do this with a simple loop that examines each score, regardless of the number of teams. Something like:
Java
int topScore = 0; // this will be used to find the highest value score
int topteam = -1; // and this will be the index of the team
for (int i = 0; i < teamNo; i++) {
    if (teamScore[i] > topScore) {
        topScore = teamScore[i]; // the highest score (up to now)
        topteam = i; // save the team number for the one with the highest score
    }
}
System.out.println(teamName[topteam] + " is the winner, with a score of " + topScore);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900