1

How could I optimise this code to take the String[] games values from the main method and have a separate method: public static int points(String[] games). I am super new to Java and don't really understand how to invoke methods.

public class TotalPoints {

    public static void main(String[] args) {
        String[] games = {"1:0","2:0","3:0","4:0","2:1","3:1","4:1","3:2","4:2","4:3"};
        int sum = 0;
        int matches = 10;
        int x = 0;
        int y = 0;
        for (int i = 0; i < games.length; i++) {
            String[] pieces = games[i].split(":");
            x = Integer.parseInt(pieces[0]);
            y = Integer.parseInt(pieces[1]);
        }

        for (int j = 0; j < matches; j++) {
            if (x > y) {
                sum = sum + 3;
            } else if (x == y) {
                sum = sum + 1;
            }
        }

        System.out.println(sum);
    }
}

3 Answers 3

1

You can write something like

public class TotalPoints {

    public static void main(String[] args) {
        int sum = points(args);
        System.out.println(sum);
    }

    public static int points(String[] games) {
        int sum = 0;
        int matches = 10;
        int x = 0;
        int y = 0;
        for (int i = 0; i < games.length; i++) {
            String[] pieces = games[i].split(":");
            x = Integer.parseInt(pieces[0]);
            y = Integer.parseInt(pieces[1]);
        }
        for (int j = 0; j < matches; j++) {
            if (x > y) {
                sum = sum + 3;
            } else if (x == y) {
                sum = sum + 1;
            }
        }
        return sum;
    }
}

And when you run this class, pass the arguments from command line like java TotalPoints "1:0" "2:0" "3:0" "4:0" "2:1" "3:1" "4:1" "3:2" "4:2" "4:3"

Sign up to request clarification or add additional context in comments.

Comments

1

very simple:

public class TotalPoints {

    public static void main(String[] args) {
       String[] games = {"1:0","2:0","3:0","4:0","2:1","3:1","4:1","3:2","4:2","4:3"};
       int result = points(games);
    }
    public static int points(String[] games) {
       //dowhat ever you want and return an int value

    }
}

Comments

1

I suggest this to you

     public class TotalPoints {
        public static void main(String[] args) {
        String[] games = {"1:0","2:0","3:0","4:0","2:1","3:1","4:1","3:2","4:2","4:3"};
        int sum = points(games);
        System.out.println(sum);
    }

    private static int points(String[] games) {
        int sum = 0;
        int matches = 10;
        int x = 0;
        int y = 0;
        for (String game : games) {
            String[] pieces = game.split(":");
            x = Integer.parseInt(pieces[0]);
            y = Integer.parseInt(pieces[1]);
        }
        for (int j = 0; j < matches; j++) {
            if (x > y) {
                sum = sum + 3;
            }
            else if (x == y) {
                sum = sum + 1;
            }
        }
        return sum;
    }
}

I replace for (int i = 0; i < games.length; i++) by for (String game : games) it's a simpler way to browse a list

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.