用java在数组中保存多个结果

用java在数组中保存多个结果,java,arrays,Java,Arrays,如何使用多个操作(如调和和)的结果填充数组:调和=1+1/2+1/3+1/4……+1/n 我的不完整版本如下所示: public static void main(String[] args) { int x=1, harmonic=0, y=2; int[] n; n = new int[]; // for populating the array ?!?!?! do {n = {x/y}} y++;

如何使用多个操作(如调和和)的结果填充数组:调和=1+1/2+1/3+1/4……+1/n 我的不完整版本如下所示:

public static void main(String[] args) {
        int x=1, harmonic=0, y=2;
        int[] n;
        n = new int[];

       // for populating the array ?!?!?!
       do {n = {x/y}} 
       y++;
       while (y<=500);

       //for the sum for loop will do...
        for (int z=0; z<=n.length; z++){
             harmonic += n[z];
            }
        System.out.println("Harmonic sum is: " + harmonic);
    }
publicstaticvoidmain(字符串[]args){
int x=1,谐波=0,y=2;
int[]n;
n=新整数[];
//为了填充阵列?!?!?!
do{n={x/y}
y++;

虽然(y2件事…您应该使用双数据类型,因为您不需要截断的值,并且您应该使用该集合而不是数组

public static void main(String[] args) {

    double x = 1, harmonic = 0, y = 2;
    List<Double> arc = new ArrayList<>();

    do {
        arc.add(x / y);
        y++;
    } while (y <= 500);

    for (Double double1 : arc) {
        harmonic += double1;
    }
    System.out.println("Harmonic sum is: " + harmonic);
}

我刚开始,还在学习基础知识,这就是为什么我尝试使用数组。我会研究你的解决方案。谢谢!
double streamedHarmonic = arc.stream().mapToDouble(Double::doubleValue).sum();