Java将命令行参数从主方法调用到单独的方法

Java将命令行参数从主方法调用到单独的方法,java,Java,我如何优化这段代码,从main方法中获取String[]games值,并拥有一个单独的方法:publicstaticintpointsstring[]games。我对Java非常陌生,不太了解如何调用方法 公共类总积分{ 公共静态无效字符串[]args{ 字符串[]游戏={1:0,2:0,3:0,4:0,2:1,3:1,4:1,3:2,4:2,4:3}; 整数和=0; int匹配=10; int x=0; int y=0; 对于int i=0;i

我如何优化这段代码,从main方法中获取String[]games值,并拥有一个单独的方法:publicstaticintpointsstring[]games。我对Java非常陌生,不太了解如何调用方法

公共类总积分{ 公共静态无效字符串[]args{ 字符串[]游戏={1:0,2:0,3:0,4:0,2:1,3:1,4:1,3:2,4:2,4:3}; 整数和=0; int匹配=10; int x=0; int y=0; 对于int i=0;iy{ 总和=总和+3; }如果x==y,则为else{ 总和=总和+1; } } System.out.printlnsum; } } 非常简单:

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

    }
}
非常简单:

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

    }
}

你可以这样写

公共类总积分{ 公共静态无效字符串[]args{ int sum=点sargs; System.out.printlnsum; } 公共静态int点字符串[]小游戏{ 整数和=0; int匹配=10; int x=0; int y=0; 对于int i=0;iy{ 总和=总和+3; }如果x==y,则为else{ 总和=总和+1; } } 回报金额; } } 运行这个类时,从命令行传递参数,如下所示
java TotalPoints 1:0 2:0 3:0 4:0 2:1 3:1 4:1 3:2 4:2 4:3

公共类总积分{ 公共静态无效字符串[]args{ int sum=点sargs; System.out.printlnsum; } 公共静态int点字符串[]小游戏{ 整数和=0; int匹配=10; int x=0; int y=0; 对于int i=0;iy{ 总和=总和+3; }如果x==y,则为else{ 总和=总和+1; } } 回报金额; } } 运行这个类时,从命令行传递参数,如下所示 java TotalPoints 1:0 2:0 3:0 4:0 2:1 3:1 4:1 3:2 4:2 4:3我建议您这样做

     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替换为int I=0;i我建议您这样做

     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替换为int I=0;i