输入一个未知长度的int数组,并计算他们的和

public static void main(String[] args) {
        System.out.println("请输入数组,用逗号隔开");
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        String[] Arrays = str.split(",");
        int[] arrays = new int[Arrays.length];
        for (int i = 0; i < arrays.length; i++) {
            arrays[i] = Integer.valueOf(Arrays[i]);
        }
        int result = add(arrays);
        System.out.println(result);
    }
    public static int add(int ...ary){
        int sum = 0 ;
       /* for (int i = 0; i < ary.length; i++) {
            sum += ary[i];
        }*/
        for (int i:ary) {
            sum += i;
        }
        return sum;
    }

你可能感兴趣的:(输入一个未知长度的int数组,并计算他们的和)