使用涉及每个元素索引的计算在数组中分配值

时间:2022-05-21 01:38:44

My array has total 10 indices and I'm trying to set array[0] = 1/1, array[1] = 1/2, array[2] = 1/3, array[4] = 1/4 and so on.

我的数组有10个索引,我试图设置数组[0] = 1/1,数组[1] = 1/2,数组[2] = 1/3,数组[4] = 1/4等等上。

After the calculation I want to display the elements with Sytem.out.println();

在计算之后我想用Sytem.out.println()显示元素;

What am I doing wrong?

我究竟做错了什么?

public static void main(String[] args) {
    final int VALUE =1;
    int[] array = new int[10];
    int counter=10;
    double result =0;
    for(int i =0; i<array.length; i++)
    {
        array[0]=VALUE/i;
        array[1]=VALUE/i;
        array[2]=VALUE/i;
        array[3]=VALUE/i;
        array[4]=VALUE/i;
        array[5]=VALUE/i;
        array[6]=VALUE/i;
        array[7]=VALUE/i;
        array[8]=VALUE/i;
        array[9]=VALUE/i;

    System.out.println( array[0] +" " + array[1] +" " + array[2] +" " + array[3]+" " +  array[4]+" "+array[5]+" " +array[6] +" " + array[7]+" " +   array[8]+" " +  array[9]+" " +  array[10]);
    }
}

2 个解决方案

#1


5  

Two problems. The first is that your loop already takes care of going over each index in the array, so you can just do something like:

两个问题。第一个是你的循环已经负责遍历数组中的每个索引,所以你可以做类似的事情:

for (int i = 0; i < array.length; i++)
{
    array[i] = 1.0/(i+1);
    System.out.println(array[i] + " ");
}

The other is that you use an array of integers. By definition, an integer cannot be a fraction. Try using an array of doubles instead:

另一个是你使用整数数组。根据定义,整数不能是分数。尝试使用一组双打:

double[] array = new double[10];

#2


4  

You are using a loop and also doing it inside manually. Just use the loops iterations to calculate it, and use the value of i for your array values.

您正在使用循环并手动执行此操作。只需使用循环迭代来计算它,并使用i的值作为数组值。

for(int i = 0; i < array.length; i++)
{
    array[i] = VALUE/(i+1);
}

System.out.println( array[0] +" " + array[1] +" " + array[2] +" " + array[3]+" " +  array[4]+" "+array[5]+" " +array[6] +" " + array[7]+" " +   array[8]+" " +  array[9]);

Another problem, as mentioned by others, it that your result would be in fraction or floating point, and you are trying to store it in an int array

另一个问题,正如其他人所提到的那样,你的结果将是分数或浮点数,并且你试图将它存储在一个int数组中

Use a float array instead

请改用float数组

float[] array = new float[10];

#1


5  

Two problems. The first is that your loop already takes care of going over each index in the array, so you can just do something like:

两个问题。第一个是你的循环已经负责遍历数组中的每个索引,所以你可以做类似的事情:

for (int i = 0; i < array.length; i++)
{
    array[i] = 1.0/(i+1);
    System.out.println(array[i] + " ");
}

The other is that you use an array of integers. By definition, an integer cannot be a fraction. Try using an array of doubles instead:

另一个是你使用整数数组。根据定义,整数不能是分数。尝试使用一组双打:

double[] array = new double[10];

#2


4  

You are using a loop and also doing it inside manually. Just use the loops iterations to calculate it, and use the value of i for your array values.

您正在使用循环并手动执行此操作。只需使用循环迭代来计算它,并使用i的值作为数组值。

for(int i = 0; i < array.length; i++)
{
    array[i] = VALUE/(i+1);
}

System.out.println( array[0] +" " + array[1] +" " + array[2] +" " + array[3]+" " +  array[4]+" "+array[5]+" " +array[6] +" " + array[7]+" " +   array[8]+" " +  array[9]);

Another problem, as mentioned by others, it that your result would be in fraction or floating point, and you are trying to store it in an int array

另一个问题,正如其他人所提到的那样,你的结果将是分数或浮点数,并且你试图将它存储在一个int数组中

Use a float array instead

请改用float数组

float[] array = new float[10];