我如何制作带有双精度数组的for循环?

问题描述 投票:-2回答:3

我正在尝试使用双精度数组创建一个for循环,但这只是行不通。

这里是代码:

double[] myDoubleArray = new double[10];
    double piValue = 3.141592;

    for (double i = 0; i < 10; piValue+) {
        myDoubleArray[i] += i;
    }

    System.out.println(myDoubleArray[2]);
    System.out.println(myDoubleArray[5]);
java arrays for-loop double
3个回答
0
投票

这是您要做什么吗?

for (int i=0; i < 10; ++i) {
    myDoubleArray[i] = Math.PI + i;
}

这将填充双值数组,从Pi开始,并为十个元素中的每个元素加一。


0
投票

即使您的数组为双精度数组,与每个位置相对应的索引仍然是int原语。

double[] doubleArray = {2.45, 4.45};

for(int i = 0; i < doubleArray.length; i++) {
     System.out.println(doubleArray[i]); //Doing something with the double value
}



0
投票

实际上用于数组索引使用int。您可以按照下面的代码

    double[] myDoubleArray = new  double[10]; 
    double piValue = 3.141592; 
    for (int i = 0; i < 10; i++) { 
          myDoubleArray[i] = piValue + i; 
    }
 System.out.println(myDoubleArray[2]);

 System.out.println(myDoubleArray[5]);
© www.soinside.com 2019 - 2024. All rights reserved.